Group by a column and select the group id on condition that applies to all the rows in it
0
votes
1
answer
89
views
I have the below table
CREATE TABLE result(
id INTEGER NOT NULL
,cid INTEGER NOT NULL
,aid INTEGER NOT NULL
,agid INTEGER NOT NULL
,ft INTEGER NOT NULL
,ct INTEGER NOT NULL
,jid INTEGER NOT NULL
,fcn INTEGER NOT NULL
,iap INTEGER NOT NULL
,flags INTEGER NOT NULL
,afcf INTEGER NOT NULL
);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (44,2,9,6,2,1698222904,52,-1,1,65536,65539);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (44,2,9,6,2,1698222904,52,-1,2,65536,4099);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (54,2,9,6,2,1698223114,56,-1,1,65536,65539);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (54,2,9,6,2,1698223114,56,-1,2,65536,4099);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (68,2,9,6,2,1698223386,59,-1,1,65536,3);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (68,2,9,6,2,1698223386,59,-1,2,65536,4099);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (80,2,9,6,2,1698223477,60,-1,1,65536,3);
INSERT INTO result(id,cid,aid,agid,ft,ct,jid,fcn,iap,flags,afcf) VALUES (80,2,9,6,2,1698223477,60,-1,2,65536,4099);
The values looks like
|id |cid|aid|agid|ft |ct |jid|fcn|iap|flags|afcf |
|---|---|---|----|---|----------|---|---|---|-----|-----|
|44 |2 |9 |6 |2 |1698222904|52 |-1 |1 |65536|65539|
|44 |2 |9 |6 |2 |1698222904|52 |-1 |2 |65536|4099 |
|54 |2 |9 |6 |2 |1698223114|56 |-1 |1 |65536|65539|
|54 |2 |9 |6 |2 |1698223114|56 |-1 |2 |65536|4099 |
|68 |2 |9 |6 |2 |1698223386|59 |-1 |1 |65536|3 |
|68 |2 |9 |6 |2 |1698223386|59 |-1 |2 |65536|4099 |
|80 |2 |9 |6 |2 |1698223477|60 |-1 |1 |65536|3 |
|80 |2 |9 |6 |2 |1698223477|60 |-1 |2 |65536|4099 |
In the above table for each of the group iap whose value here is
[1,2]
. I want to get that iap where afcf & 65536 = 0
else I want the value to be NULL
.
So my output would contain 2
for above table as all the rows in iap 2 satisfy the condition `afcf & 65536 = 0.
**What needs to be done**
For each of the group of iap, check if all its rows satisfies the condition afcf & 65536 = 0
if yes then print that group id iap
.
**Expected Output**
Since iap = satisfied above condition for all of its rows
2
This is what I tried, which is wrong.
SELECT iap FROM result
GROUP BY iap
HAVING afcf & 65536 = 0
Asked by Himanshuman
(197 rep)
Nov 2, 2023, 08:26 AM
Last activity: Nov 2, 2023, 05:13 PM
Last activity: Nov 2, 2023, 05:13 PM