SQL for set of all in group by - sql

Assume I have table foo
A B C
==============
1 1 1
1 2 3
1 2 4
1 3 6
2 2 6
I want the set of all C where I have a duplicate AB. Something like:
select all(C) from foo group by a, b having count(b) > 1
I want the result to be
all(C)
===
3
4
Is there an easy way to do this in Oracle SQL?

SELECT t1.c
FROM foo t1
JOIN foo t2 ON (t1.a = t2.a AND
t1.b = t2.b AND
t1.rowid != t2.rowid)
should give you what you're after. A bit more efficient would likely be to use an analytic function
SELECT c
FROM (SELECT f.*,
count(*) over (partition by a, b) cnt
FROM foo f)
WHERE cnt > 1

Try this:
SELECT C
FROM
(select C, COUNT(*) OVER(PARTITION BY A, B) AS DUPLICATES
from MY_TABLE) AS RESULTS
WHERE DUPLICATES > 1

I figured it out. I used
select f.c from foo f
join (select a, b from foo group by a, b having count(c) > 1) dupes
on dupes.a = f.a and dupes.b = f.b;

Related

How to select rows by max value from another column in Oracle

I have two datasets in Oracle Table1 and Table2.
When I run this:
SELECT A.ID, B.NUM_X
FROM TABLE1 A
LEFT JOIN TABLE2 B ON A.ID=B.ID
WHERE B.BOOK = 1
It returns this.
ID NUM_X
1 10
1 5
1 9
2 2
2 1
3 20
3 11
What I want are the DISTINCT ID where NUM_X is the MAX value, something like this:
ID NUM_x
1 10
2 2
3 20
You can use aggregation:
SELECT A.ID, MAX(B.NUM_X)
FROM TABLE1 A LEFT JOIN
TABLE2 B
ON A.ID = B.ID
WHERE B.BOOK = 1
GROUP BY A.ID;
If you wanted additional columns, I would recommend window functions:
SELECT A.ID, MAX(B.NUM_X)
FROM TABLE1 A LEFT JOIN
(SELECT B.*,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY NUM_X DESC) as seqnum
FROM TABLE2 B
) B
ON A.ID = B.ID AND B.seqnum = 1
WHERE B.BOOK = 1
GROUP BY A.ID;

SQL Select group where some attribute is same

Lets say I have a table like this
A B C
-----
1 a 12
2 a 23
3 b 43
4 c 25
5 c 44
6 d 34
How to select only rows where B exists in another row?
Result would be:
A B C
-----
1 a 12
2 a 23
4 c 25
5 c 44
I'm not sure what you are expecting but eliminating B & D
we can achieve like this
Select T.A,T.B,T.C from Table T
INNER JOIN (
SELECT B FROM Table
groUP by b
having count(B) > 1 )TT
ON T.B = TT.B
Just use exists:
select t.*
from t
where exists (select 1
from t t2
where t2.b = t.b and t2.a <> t.a
);
With an index on t(b, a), this is likely to be the fastest method.

How to find rows missing by every group

I have two tables:
Input:
A:
ID col
1 a
1 b
1 c
2 a
2 b
3 x
4 y
B
ID col
1 a
1 b
2 a
I want to for every ID in B, find rows in A but not in B by every ID.
Output:
ID Col
1 c
2 b
What I tried:
left/right join. I am trying something like select * from a left join b on a.id = b.id where b.id is null
except. select * from a except select * from b
but not sure how to modify it.
Assuming you want the values in A for which there are records in B with the same ID, but not the same col, you could do:
select
a.ID,
a.col
from A
left join B
on b.ID = a.ID and b.col = a.col
where A.ID in (select distinct ID from B) -- B contains this `ID` somewhere...
and B.ID is null -- ...but not with the same `col`
Test it here.
Using a combination of exists and not exists.
select *
from a
where exists (select 1 from b where a.id=b.id) --id check
and not exists (select 1 from b where a.id=b.id and a.col=b.col) -- col check

How to get an ID associated with at least all contents?

Suppose we have the database:
-----------
| A -|- B |
|----|----|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
-----------
Where A and B is the primary key. Suppose we want to get all As that contain the elements in B of 1 and 2.
SELECT A FROM Table WHERE B = 1 AND B = 2;
The above fails because it never holds true as the query is only for a single record.
SELECT A FROM Table WHERE B = 1 OR B = 2;
Works but erroneously includes the primary key value 2, which only maps to 1 in B, and not both 1 and 2 in B.
GROUP BY solution, return all a's that have more than 1 different b value in (1,2):
select a from table
where b in (1,2)
group by a
having count(distinct b) > 1
Or, JOIN solution:
select distinct a
from (select a from table where b = 1) t1
join (select a from table where b = 2) t2
on t1.a = t2.a
Or an INTERSECT solution:
select a from table where b = 1
intersect
select a from table where b = 2
Edit:
GROUP BY query that perhaps is faster then the HAVING count distinct version:
select a from table
where b in (1,2)
group by a
having max(b) <> min(b)
You can use the group by method from jarlh or make a Join with a 'distinct':
select distinct a
from (select a from table where b = 1) t1
join (select a from table where b = 2) t2
on t1.a = t2.a
Something like this (assuming that you need to filter by the specific IDs in B.
SELECT DISTINCT A
FROM Table AS T
WHERE EXISTS (SELECT 1 from Table WHERE Table.A = T.A and B = 1)
AND EXISTS (SELECT 1 from Table WHERE Table.A = T.A and B = 2)
Try this
SELECT A
FROM Table
WHERE EXISTS (
SELECT 1
FROM Table t1
WHERE t1.A = Table.A
AND t1.B = 1
)
AND EXISTS (
SELECT 1
FROM Table t2
WHERE t2.A = Table.A
AND t2.B = 2
)
A cannot be the primary key here, since the column contains duplicates.
One possible solution:
SELECT * FROM (SELECT A, group_concat(B, ',') AS C FROM tab GROUP BY A) s WHERE s.C = "1,2";

Combination of group by, order and distinct

My query
SELECT a, b, c
FROM table
WHERE
a > 0 AND a < 4 AND
b IN (
SELECT z FROM table2
WHERE x = y
)
produces the following output:
A B C
1 1 Car
1 1 Keyboard
1 2 Apple
1 3 Frog
2 1 Carrot
2 2 Parrot
3 1 Doll
what I want is the following output
A B C
1 1 Car
2 1 Carrot
3 1 Doll
So basically for every A, the lowest B and associated C (as well as other columns).
I tried various join types, group bys, but I am running out of ideas.
How can I accomplish this?
Use a Top N Apply
SELECT a, b, c
FROM table
CROSS APPLY (SELECT top 1 z
FROM table2
WHERE x = y
order by z ) t2
WHERE a > 0 AND a < 4 AND
Do a join on a subquery:
SELECT a, b, c
FROM table t1
INNER JOIN (SELECT a a2, MIN(b) b2 FROM table GROUP BY a) t2
ON t1.a = t2.a2 AND t1.b = t2.b2
WHERE
a > 0 AND a < 4 AND
b IN (
SELECT z FROM table2
WHERE x = y
)