Performing sum per row based on conditions SQL - sql

I wanted to know how I can perform a sum per row based on conditions for the columns using SQL (I'm new to SQL).
For example, I have this table:
ID Col_1 Col_2 Col_3 ...
1 L L L ...
2 L Q Q ...
3 L L Q ...
4 Q Q L ...
The result that I'm looking for is:
ID count_L count_Q
1 3 0
2 1 2
3 2 1
4 1 2
I'm not sure on how I should approach this. Doing this using Count function if my table was transposed would be easier (I think) but performing the query in the way my data is organized is tricky for me. I think I need nested SQL statements and join them using UNION but not sure how to do it.
I wasn't able to find similar questions/solutions elsewhere. Would appreciate some help!

You can use iif() and +:
select id,
(iif(col_1 = "L" , 1, 0) + iif(col_2 = "L" , 1, 0) + iif(col_3 = "L" , 1, 0) ) as count_l,
(iif(col_1 = "Q" , 1, 0) + iif(col_2 = "Q" , 1, 0) + iif(col_3 = "Q" , 1, 0) ) as count_q
from t;

Related

postgresql Multiple identical conditions are unified into one parameter

I have one sql that need convert string column to array and i have to filter with this column,sql like this:
select
parent_line,
string_to_array(parent_line, '-')
from
bx_crm.department
where
status = 0 and
'851' = ANY(string_to_array(parent_line, '-')) and
array_length(string_to_array(parent_line, '-'), 1) = 5;
parent_line is a varchar(50) column,the data in this like 0-1-851-88
question:
string_to_array(parent_line, '-') appear many times in my sql.
how many times string_to_array(parent_line) calculate in each row. one time or three times
how convert string_to_array(parent_line) to a parameter. at last,my sql may like this:
depts = string_to_array(parent_line, '-')
select
parent_line,
depts
from
bx_crm.department
where
status = 0 and
'851' = ANY(depts) and
array_length(depts, 1) = 5;
Postgres supports lateral joins which can simplify this logic:
select parent_line, v.parents, status, ... other columns ...
from bx_crm.department d cross join lateral
(values (string_to_array(parent_line, '-')) v(parents)
where d.status = 0 and
cardinality(v.parents) = 5
'851' = any(v.parents)
Use a derived table:
select *
from (
select parent_line,
string_to_array(parent_line, '-') as parents,
status,
... other columns ...
from bx_crm.department
) x
where status = 0
and cardinality(parents) = 5
and '851' = any(parents)

May I know how can I construct the follow query in SQL Server?

CREATE TABLE (
A INT NOT NULL,
B INT NOT NULL
)
A is an enumerated values of 1, 2, 3, 4, 5
B can be any values
I would like to count() the number of occurrence group by B, with a specific subset of A e.g. {1, 2}
Example:
A B
1 7 *
2 7 *
3 7
1 8 *
2 8 *
1 9
3 9
When B = 7, A = 1, 2, 3. Good
When B = 8, A = 1, 2. Good
When B = 9, A = 1, 3. Not satisfy, 2 is missing
So the count will be 2 (when B = 7 and 8)
If I've understood you correctly, we want to find B values for which we have both a 1 and a 2 in A, and then we want to know how many of those we have.
This query does this:
declare #t table (A int not null, B int not null)
insert into #t(A,B) values
(1,7),
(2,7),
(3,7),
(1,8),
(2,8),
(1,9),
(3,9)
select COUNT(DISTINCT B) from (
select B
from #t
where A in (1,2)
group by B
having COUNT(DISTINCT A) = 2
) t
One or both of the DISTINCTs may be unnecessary - it depends on whether your data can contain repeating values.
If I understand correctly and the requirement is to find Bs with a series of As that doesn't have any "gaps", you could compare the difference between the minimal and maximal A with number of records (per B, of course):
SELECT b
FROM mytable
GROUP BY b
HAVING COUNT(*) + 1 = MAX(a) - MIN(a)
SELECT COUNT(DISTINCT B) FROM TEMP T WHERE T.B NOT IN
(SELECT B FROM
(SELECT B,A,
LAG (A,1) OVER (PARTITION BY B ORDER BY A) AS PRE_A
FROM Temp) K
WHERE K.PRE_A IS NOT NULL AND K.A<>K.PRE_A+1);

Integer Value Right Padding in SQL

Step1: I have a table called XYZ which contains following integer columns:
ID A B C
1 201507 20150810 20150311
2 20150812 201509 201510
I need to write a SQL query where if any values of A, B, and C is smaller than 8 digits then I need to consider it as 8 digits by adding zeros to the right of the value for step2 (I am not allowed to update the table.). For example:
ID A B C
1 20150700 20150810 20150311
2 20150812 20150900 20151000
How to add zeros to the right of the integer values through SQL query?
Step 2: I need to find for each record A<B, B<C or not. Please let me know how to write the query. I am using PostgreSQL. Thank you.
SELECT CAST(2015 AS VARCHAR(10))+REPLICATE('0',8-LEN(2015))
SELECT 2015 *(CAST('1'+REPLICATE('0',8-len(2015)) AS INT))
You can use rpad() to add trailing zeros, then cast the result back to an integer:
select id,
rpad(a::text, 8, '0')::int,
rpad(b::text, 8, '0')::int,
rpad(c::text, 8, '0')::int
from the_table;
To avoid repeating the expressions, use a derived table:
select *
from (
select id,
rpad(a::text, 8, '0')::int as a,
rpad(b::text, 8, '0')::int as b,
rpad(c::text, 8, '0')::int as c
from the_table
) t
where a < b or b < c --<< change that to the condition you want
just try this
select
ID,
A = LEFT(cast(a as varchar(100)+'00000000',8),
b = LEFT(cast(b as varchar(100)+'00000000',8),
C = LEFT(cast(c as varchar(100)+'00000000',8)
from xyz
Try this:
select cast(left(cast(A as varchar(20)) + '00000000', 8) as int) as [A],
cast(left(cast(B as varchar(20)) + '00000000', 8) as int) as [B],
cast(left(cast(C as varchar(20)) + '00000000', 8) as int) as [C]
from TABLE_NAME
If you want to avoid any casting, this might be solution:
select case when 8 - LEN(A) > 0 then A * Power(10, (8 - LEN(A))) else A end as [A],
case when 8 - LEN(B) > 0 then B * Power(10, (8 - LEN(B))) else B end as [B],
case when 8 - LEN(C) > 0 then C * Power(10, (8 - LEN(C))) else C end as [C]
from MY_TABLE

Nested conditions in sql

I have the where condition in the sql:
WHERE
( Spectrum.access.dim_member.centene_ind = 0 )
AND
(
Spectrum.access.Client_List_Groups.Group_Name IN ( 'Centene Health Plan Book of Business' )
AND
Spectrum.access.dim_member.referral_route IN ( 'Claims Data' )
AND
***(
Spectrum.access.fact_task_metrics.task = 'Conduct IHA'
AND
Spectrum.access.fact_task_metrics.created_by_name <> 'BMU, BMU'
AND
Spectrum.access.fact_task_metrics.created_date BETWEEN '01/01/2015 00:0:0' AND '06/30/2015 00:0:0'
)***
AND
***(
Spectrum.access.fact_outreach_metrics.outreach_type IN ( 'Conduct IHA' )
AND
(
Spectrum.dbo.ufnTruncDate(Spectrum.access.fact_outreach_metrics.metric_date) >= Spectrum.access.fact_task_metrics.metric_date
OR
Spectrum.access.fact_outreach_metrics.metric_date >= Spectrum.access.fact_task_metrics.created_date
)
)***
AND
Spectrum.access.fact_outreach_metrics.episode_seq = 1
AND
Spectrum.access.dim_member.reinstated_date Is Null
)
I have marked two of the conditions in the above code.
The 1st condition have 2 AND operators.
The 2nd condition has an AND and an OR operator.
Question 1: Does removing the outer brackets "(" in the 1st condition impact the results?
Question 2: Does removing the outer brackets "(" in the 2nd condition impact the results?
After removing the outer bracket the filters will look like:
Spectrum.access.dim_member.referral_route IN ( 'Claims Data' )
AND
Spectrum.access.fact_task_metrics.task = 'Conduct IHA'
AND
Spectrum.access.fact_task_metrics.created_by_name <> 'BMU, BMU'
AND
Spectrum.access.fact_task_metrics.created_date BETWEEN '01/01/2015 00:0:0' AND '06/30/2015 00:0:0'
AND
Spectrum.access.fact_outreach_metrics.outreach_type IN ( 'Conduct IHA' )
AND
(
Spectrum.dbo.ufnTruncDate(Spectrum.access.fact_outreach_metrics.metric_date) >= Spectrum.access.fact_task_metrics.metric_date
OR
Spectrum.access.fact_outreach_metrics.metric_date >= Spectrum.access.fact_task_metrics.created_date
)
AND
Spectrum.access.fact_outreach_metrics.episode_seq = 1
Appreciate your help.
Regards,
Jude
Order of operations dictate that AND will be processed before OR when these expressions are evaluated within a parenthesis set.
WHERE (A AND B) OR (C AND D)
Is equivalent to:
WHERE A AND B OR C AND D
But the example below:
WHERE (A OR B) AND (C OR D)
Is not equivalent to:
WHERE A OR B AND C OR D
Which really becomes:
WHERE A OR (B AND C) OR D
Technically, you should be able to safely remove the parenthesis in question for both of your examples. With the AND statement, you are adding all of your conditions together to be one large condition. When using the OR clause, you should carefully place the parenthesis so that the groups are properly segmented.
Take the following examples into consideration:
a) where y = 1 AND n = 2 AND x = 3 or x = 5
b) where y = 1 AND n = 2 AND (x = 3 or x = 5)
c) where (y = 1 AND n = 2 AND x = 3) or x = 5
In example A, the intended outcome is unclear.
In example B, the intended outcome states that all of the conditions must be met and X can be either 3 or 5.
In example C, the intended outcome states that either Y=1, N=2 and X=3 OR x=5. As long as X = 5, it doesn't matter what Y and N equal.

SQL - Pulling data from the same table with two different "where" statements

I have a table with the following columns...
TestName - StepNumber - Data_1
I'm trying to write a query that can look for Data_1 results and average them for one day. The TestNames are unique tests we're running, and StepNumbers are the individual steps inside of the test. Normally, I would use something like
select Data_1 from table
where TestName in(1,2,3,4)
and StepNumber in(1)
to return all of the Data_1 results I need. However, sometimes the data I need is located in different steps across the table. Test 1 might have the required data in Step 2, Test 2 in step 10, etc...and in the end, I need an average of the Data_1 results for all of the similar StepNumber results. I'm not sure how I can capture this data in a single query. There's a separate part of the query where I'm breaking it down by geography, and doing it individually would take a long time.
I'd be looking for something like...
select avg(Data_1) from table
where TestName = 1 and StepNumber = 2
and TestName = 2 and StepNumber = 10
and TestName = 3 and StepNumber = 5
If I can clarify, please let me know. Thank you!
select avg(Data_1)
from table
where (TestName = 1 and StepNumber = 2)
or (TestName = 2 and StepNumber = 10)
or (TestName = 3 and StepNumber = 5)
If I have understood correctly, you have three (or more) sets of data in your table:
TestName = 1 and StepNumber = 2 - cardinality N1
Testname = 2 and StepNumber = 10 - cardinality N2
TestName = 3 and StepNumber = 5 - cardinality N3
If you want the average for all three separately, you needs must select three columns. AVG in this case cannot help you as it would run the average on a cardinality of N4, this being the intersection of the three groups. So you have to do this by hand. I do not know how this would perform vs. three separate queries:
SELECT
AVG(Data_1) AS OverallAverage,
SUM(IF((TestName = 1 AND StepNumber = 2), Data_1, 0))
/SUM(IF((TestName = 1 AND StepNumber = 2), 1, 0)) AS AvgGroup1,
SUM(IF((TestName = 2 AND StepNumber = 10), Data_1, 0))
/SUM(IF((TestName = 2 AND StepNumber = 10), 1, 0)) AS AvgGroup2,
SUM(IF((TestName = 3 AND StepNumber = 5), Data_1, 0))
/SUM(IF((TestName = 3 AND StepNumber = 5), 1, 0)) AS AvgGroup3
FROM table
WHERE (
( TestName = 1 AND StepNumber = 2)
OR
( TestName = 2 AND StepNumber = 10)
OR
( TestName = 3 AND StepNumber = 5)
);
This kind of query can be assembled from components, i.e. programmatically depending on the groups.
This is a SQLFiddle to show the results.