Find records with column containing specific values - sql

I think my question really boils down do doing a sql IN on multiple values.
Here's a SQL Fiddle of what I have so far.
So long story short, I have Cases that have multiple Types.
I need to filter Cases containing certain Types
Say I have 6 rows (col1,col2)... (1,2),(1,3)(1,4),(2,3),(2,4),(2,5)
How do I select only Cases that have 2, AND 3 AND 4 in col2?
2,3,4 is just a use case. It could be just about any combination (the SQL Fiddle has a couple tables that determine this).

Try:
select [CaseId]
from CaseTypes
where [TypeID] in (2,3,4)
group by [CaseId]
having count(distinct [TypeID]) = 3
(SQLFiddle here)

Couple of things you might try:
Using Derived Tables:
SELECT a.col1
FROM (SELECT col1 FROM Cases WHERE col2 = 2) a
INNER JOIN (SELECT col1 FROM Cases WHERE col2 = 3) b ON a.col1 = b.col1
INNER JOIN (SELECT col1 FROM Cases WHERE col2 = 4) c ON a.col1 = c.col1
Using Aggregation:
SELECT col1
FROM Cases
GROUP BY col1
HAVING
SUM(CASE col2 WHEN 2 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE col2 WHEN 3 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE col2 WHEN 4 THEN 1 ELSE 0 END) > 0

is this a bit closer?
SELECT * from CaseTypes CT
INNER JOIN TypeBillable TB ON CT.TypeId = TB.TypeId
where CT.CaseId in
(
select CaseID from CaseTypes
PIVOT (count (TypeId) for TypeId in ([2],[3],[4])) as x
where [2] > 0 and [3] > 0 and [4] > 0
)

Related

Break out nested data within SQL, criteria across multiple rows (similar to dcast in R)

I'm trying to write a simple query to take a data set that looks like this:
ID | Col2
X B
X C
Y B
Y D
and return this:
ID | Col2 | Col3
X B C
Y B D
Essentially, I have an ID column that can have either B, C, or D in Col2. I am trying to identify which IDs only have B and D. I have a query to find both, but not only that combination. Query:
select ID, Col2
from Table1
where ID in (
select ID from Table1
group by ID
having count(distinct Col2) = 2)
order by ID
Alternatively, I could use help in finding a way to filter that query on B and D and leave off B and C. I have seen perhaps a self join, but am not sure how to implement that.
Thanks!
EDIT: Most of the data set has, for a given ID, all three of B, C, and D. The goal here is to isolate the IDs that are missing one, namely missing C.
I am trying to identify which IDs only have B and D. I have a query to find both
If this is what you want, you don't need multiple columns:
select id
from table1
where col2 in ('B', 'D')
group by id
having count(distinct col2) = 2;
If you want only 'B' and 'D' and no others, then:
select id
from table1
group by id
having sum(case when col2 = 'B' then 1 else 0 end) > 0 AND
sum(case when col2 = 'C' then 1 else 0 end) > 0 AND
sum(case when col2 not in ('B', 'D') then 1 else 0 end) = 0;
If there are only two columns, you can also easily pivot the values using aggregation:
select id, min(col2), nullif(max(col2), min(col2))
from table1
group by id;

Get single row depending of conditional

I have a simple select query with some joins like:
SELECT
[c].[column1]
, [c].[column2]
FROM [Customer] AS [c]
INNER JOIN ...
So I do a left join with my principal table as:
LEFT JOIN [Communication] AS [com] ON [c].[CustomerGuid] = [com].[ComGuid]
this relatioship its 1 to *, one customer can have multiple communications
So in my select I want to get value 1 or 2 depending of condition:
Condition:
if ComTypeKey (from communication) table have a row with value 3 and have another row with vale 4 return 1 then 0
So I try something like:
SELECT
[c].[column1]
, [c].[column2]
, IIF([com].[ComTypeKey] = 3 AND [com].[ComTypeKey] = 4,1,0)
FROM [Customer] AS [c]
INNER JOIN ...
LEFT JOIN [Communication] AS [com] ON [c].[CustomerGuid] = [com].[ComGuid]
But it throws me two rows, beacause there are 2 rows on communication. My desire value is to get only one row with value 1 if my condition is true
If you have multiple rows you need GROUP BY, then count the relevant keys and subtract 1 to get (1, 0)
SELECT
[c].[column1]
, [c].[column2]
, COUNT(CASE WHEN [ComTypeKey] IN (3,4) THEN 1 END) - 1 as FLAG_CONDITION
FROM [Customer] AS [c]
INNER JOIN ...
LEFT JOIN [Communication] AS [com]
ON [c].[CustomerGuid] = [com].[ComGuid]
GROUP BY
[c].[column1]
, [c].[column2]
I'm not really sure I understand.
This will literally find if both values 3 and 4 exist for that CustomerGuid, and only select one of them in that case - not filtering out any record otherwise.
If this is not what you want, providing sample data with the expected result would remove the ambiguity.
SELECT Field1,
Field2,
...
FieldN
FROM (SELECT TMP.*,
CASE WHEN hasBothValues = 1 THEN
ROW_NUMBER() OVER ( PARTITION BY CustomerGuid ORDER BY 1 )
ELSE 1
END AS iterim_rn
FROM (SELECT TD.*,
MAX(CASE WHEN Value1 = '3' THEN 1 ELSE 0 END) OVER
( PARTITION BY CustomerGuid ) *
MAX(CASE WHEN Value1 = '4' THEN 1 ELSE 0 END) OVER
( PARTITION BY CustomerGuid ) AS hasBothValues
FROM TEST_DATA TD
) TMP
) TMP2
WHERE interim_rn = 1

Output of Multiple Select Statement In Single Row

I have multiple query which is providing count based on different where condition.
Will it be possible to bring all result in single Rows.
for eg:
Query1:
SELECT COUNT(COL25) ASSURED, FROM TAB1 WHERE COL1= 'ALPHA' AND COL2='ROLE'
Query2:
SELECT COUNT(COL25) RELEASE FROM TAB1 WHERE COL3 in('BEETA','X','Y') AND COLSTATUS='ABC'
The result for 1st query is ASSURED = 100
and 2nd Query is RELEASE = 5000
i am trying to display output as
ASSURED | RELEASE
100 | 5000
Use conditional aggregation:
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
EDIT:
If you actually need to count non-NULL values, you can be explicit (my preference):
SELECT SUM(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as ASSURED,
SUM(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' AND col25 IS NOT NULL THEN 1 ELSE 0 END) as RELEASE
FROM TAB1;
Or be a bit more implicit:
SELECT COUNT(CASE WHEN COL1 = 'ALPHA' AND COL2 = 'ROLE' THEN col25 END) as ASSURED,
COUNT(CASE WHEN COL3 = 'BEETA' AND COLSTATUS = 'ABC' THEN col25 END) as RELEASE
FROM TAB1;
You can also use joins, but #Gordon Linoff answer is cleaner and way shorter than this.
SELECT first.ASSURED, second.RELEASE
FROM
(SELECT
COUNT(COL25) ASSURED
FROM TAB1
WHERE COL1= 'ALPHA' AND COL2='ROLE') AS first
INNER JOIN (SELECT
COUNT(COL25) RELEASE
FROM TAB1
WHERE COL3 in('BEETA','X','Y')
AND COLSTATUS='ABC') AS second
ON 1=1

SQL (TSQL) - Select values in a column where another column is not null?

I will keep this simple- I would like to know if there is a good way to select all the values in a column when it never has a null in another column. For example.
A B
----- -----
1 7
2 7
NULL 7
4 9
1 9
2 9
From the above set I would just want 9 from B and not 7 because 7 has a NULL in A. Obviously I could wrap this as a subquery and USE the IN clause etc. but this is already part of a pretty unique set and am looking to keep this efficient.
I should note that for my purposes this would only be a one-way comparison... I would only be returning values in B and examining A.
I imagine there is an easy way to do this that I am missing, but being in the thick of things I don't see it right now.
You can do something like this:
select *
from t
where t.b not in (select b from t where a is null);
If you want only distinct b values, then you can do:
select b
from t
group by b
having sum(case when a is null then 1 else 0 end) = 0;
And, finally, you could use window functions:
select a, b
from (select t.*,
sum(case when a is null then 1 else 0 end) over (partition by b) as NullCnt
from t
) t
where NullCnt = 0;
The query below will only output one column in the final result. The records are grouped by column B and test if the record is null or not. When the record is null, the value for the group will increment each time by 1. The HAVING clause filters only the group which has a value of 0.
SELECT B
FROM TableName
GROUP BY B
HAVING SUM(CASE WHEN A IS NULL THEN 1 ELSE 0 END) = 0
If you want to get all the rows from the records, you can use join.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT B
FROM TableName
GROUP BY B
HAVING SUM(CASE WHEN A IS NULL THEN 1 ELSE 0 END) = 0
) b ON a.b = b.b

Get the distinct count of values from a table with multiple where clauses

My table structure is this
id last_mod_dt nr is_u is_rog is_ror is_unv
1 x uuid1 1 1 1 0
2 y uuid1 1 0 1 1
3 z uuid2 1 1 1 1
I want the count of rows with:
is_ror=1 or is_rog =1
is_u=1
is_unv=1
All in a single query. Is it possible?
The problem I am facing is that there can be same values for nr as is the case in the table above.
Case statments provide mondo flexibility...
SELECT
sum(case
when is_ror = 1 or is_rog = 1 then 1
else 0
end) FirstCount
,sum(case
when is_u = 1 then 1
else 0
end) SecondCount
,sum(case
when is_unv = 1 then 1
else 0
end) ThirdCount
from MyTable
you can use union to get multiple results e.g.
select count(*) from table with is_ror=1 or is_rog =1
union
select count(*) from table with is_u=1
union
select count(*) from table with is_unv=1
Then the result set will contain three rows each with one of the counts.
Sounds pretty simple if "all in a single query" does not disqualify subselects;
SELECT
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_ror=1 OR is_rog=1) cnt_ror_reg,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_u=1) cnt_u,
(SELECT COUNT(DISTINCT nr) FROM table1 WHERE is_unv=1) cnt_unv;
how about something like
SELECT
SUM(IF(is_u > 0 AND is_rog > 0, 1, 0)) AS count_something,
...
from table
group by nr
I think it will do the trick
I am of course not sure what you want exactly, but I believe you can use the logic to produce your desired result.