problem with select - sql

I have a table with rows with two columns
A 1
A 2
B 1
B 3
C 1
C 2
C 3
and I want to get from this only this ID(a,b or c) which has only 2 rows with value 1,2, so from this table I should get a, bacause b hasn't row with 2, and c has rows with 1 and b, but also has row with c..
What is the simplest way to get this row?

SELECT col1
FROM YourTable
GROUP BY col1
HAVING COUNT(DISTINCT col2) =2 AND MIN(col2) = 1 AND MAX(col2) = 2
Or another way extendible to more than 2 numbers
SELECT col1
FROM yourtable
GROUP BY col1
HAVING MIN(CASE
WHEN col2 IN ( 1, 2 ) THEN 1
ELSE 0
END) = 1
AND COUNT(DISTINCT col2) = 2

select t1.col1
from table as t1
left join table as t2 on (t1.col1 = t2.col1)
where t1.col2 = 1 and t2.col2 = 2;

Related

how to extract the rows where a group appears more than a certain number of times

I have the following table
col1 col2 col3 key
A B C 1
A B B 2
A B B 3
A B D 4
B D C 5
I would like to extract the rows where the group col1, col2, col3 appears more than once in the table.
A B B 2
A B B 3
So far, I have:
SELECT col1, col2, col3, count(*)
FROM db.table
GROUP BY col1, col2, col3
HAVING count(*) > 1
col1 col2 col3 count(*)
A B B 2
Is there a way to extract those rows with A B B without having to join the final table with the initial table?
You could use exists logic:
SELECT col1, col2, col3, "key"
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable t2
WHERE t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND
t2.col3 = t1.col3 AND
t2."key" <> t1."key");
Try below query with CTE
with MyCTE
as
(
select col1,col2,col3,Key,COUNT(*) over(PARTITION BY col1,col2,col3 order
by col1,col2,col3) as Duplicate from yourtable
)
select col1,col2,col3,key from MyCTE where Duplicate>1

Filter in SQL on distinct values after grouping

I have a dataset like
col1 col2 col3
A x 1
A x 2
A x 3
B y 4
B -y 5
B y 6
C -z 7
C z 8
C -z 9
D t 10
D t 11
D t 12
how can i pick out just the groups from col1 that have distinct values in col2? So A,D in this case.
something like
select * from table t1
where (select count(distinct col2)
from table t2
where t1.col1 = t2.col1) > 1
but more optimized?
If all you need is the column col1 you can group by col1 and set the condition in the HAVING clause:
SELECT col1
FROM tablename
GROUP BY col1
HAVING COUNT(DISTINCT col2) = 1;
If you want all the rows from the table use the above query with the operator IN:
SELECT *
FROM tablename
WHERE col1 IN (
SELECT col1
FROM tablename
GROUP BY col1
HAVING COUNT(DISTINCT col2) = 1
)
You can use group by and having:
select col1
from t
group by col1
having min(col2) <> max(col2);

Return rows where specific column has duplicate values

From the table below I want to show the two rows where the values in column 3 are duplicates:
ID
Col2
Col3
1
a
123
2
b
123
3
c
14
4
d
65
5
e
65
This means that the query that I need should return rows with ID 1, 2 and 4, 5.
I wrote query using having:
SELECT *
FROM t1
INNER JOIN (SELECT col3 FROM t1
GROUP BY col3
HAVING COUNT(*) > 1) a
ON t1.col3 = a.col3
This query though only returns 1 and 4 rows for example, not all duplicates.
I would appreciate the help.
Your query should work, but I would suggest window functions:
select t1.*
from (select t1.*, count(*) over (partition by col3) as cnt
from t1
) t1
where cnt > 1;

Select Group data with one matching condition

Table:
Col1 Col2
1 2
1 3
1 4
2 2
2 3
first need to check all rows with col2 = 4
Then need to select all rows with values col1
The result should be:
1 2
1 3
1 4
Off the top of my head
SELECT A.* FROM MyTable A JOIN MyTable B ON A.Col1 = B.Col1 WHERE B.Col2 = 4
I think you want this:
select t.*
from t
where t.col1 in (select t2.col1 from t t2 where t2.col2 = 4);
This query checks on both columns, where col2 = 4 and col1 = 1, from what i can understand in your description.
SELECT t1.col1, t2.col2 FROM Table t1
WHERE t1.col2 = 4
UNION
SELECT t2.col1, t2.col2 FROM Table t2
WHERE t2.col1 = 1

SQL where column equals Val and equals Val2

If you have a table as per bellow:
Col1 Col2
1 1
2 1
3 1
1 2
3 2
2 3
is there a way to write a SQL query where (Col2 =1 and Col2=2) and the result is:
Col1
1
3
You could do this using intersect. Note that this wouldn't work in MySQL.
select col1 from tablename where col2 = 1
intersect
select col1 from tablename where col2 = 2
You can use HAVING with CASE WHEN:
SELECT Col1
FROM your_table
GROUP BY Col1
HAVING SUM(CASE WHEN Col2 = 1 THEN 1 END) > 0
AND SUM(CASE WHEN Col2 = 2 THEN 1 END) > 0;
LiveDemo
With MySQL you can write:
SELECT Col1
FROM your_table
GROUP BY Col1
HAVING SUM(Col2 = 1) > 0
AND SUM(Col2 = 2) > 0;
Self join version:
select distinct t1.col1
from (select col1 from tablename where col2 = 1) t1
join (select col1 from tablename where col2 = 2) t2 on t1.col1 = t2.col1