SQL get all IDs where Sub-IDs are exactly specified without getting other IDs where some Sub-ID's are not present - sql

Sorry for that title, I don't know how to describe my problem in one sentence.
I have Table like this:
event | thema
-------------
1 1
1 2
2 1
2 2
2 3
3 1
3 2
3 3
3 4
4 1
4 2
4 3
What I want are the event IDs where the thema is exaclty 1, 2 and 3, not the event ID where it is only 1 and 2 or 1,2,3 and 4.
SELECT event WHERE thema=1 OR thema=2 OR thema=3
returns them all
SELECT event WHERE thema=1 AND thema=2 AND thema=3
returns nothing.
I think this should be absolutely simple, but stack is overflown...
Thanks for some help!

Group by the event and take only those having at least one thema 1 and 2 and 3 and not any other
SELECT event
from your_table
group by event
having sum(case when thema = 1 then 1 else 0 end) > 0
and sum(case when thema = 2 then 1 else 0 end) > 0
and sum(case when thema = 3 then 1 else 0 end) > 0
and sum(case when thema not in (1,2,3) then 1 else 0 end) = 0

This type of query is a "set-within-sets" query (your are looking for sets of "thema" for each event). The most general approach is aggregation using a having clause. This might be the shortest way to write the query using standard SQL:
select event
from table t
group by event
having count(distinct (case when thema in (1, 2, 3) then thema end)) = 3;

or,
first create table #themas (depending on vendor, make this a temp table or a simple table-valued variable) that contains user-specified list of thema values, then
Select event from your_table y
Where not exists
(Select * From #Themas t
where Not Exists
(Select * From your_table
where event = y.event
and thema = t.thema))
and not exists (Select * From your_table
where event = t.event
and thema not in
(Select thema From #Themas ))

Related

How to check the count of each values repeating in a row

I have two tables. Data in the first table is:
ID Username
1 Dan
2 Eli
3 Sean
4 John
Second Table Data:
user_id Status_id
1 2
1 3
4 1
3 2
2 3
1 1
3 3
3 3
3 3
. .
goes on goes on
These are my both tables.
I want to find the frequency of individual users doing 'status_id'
My expected result is:
username status_id(1) status_id(2) status_id(3)
Dan 1 1 1
Eli 0 0 1
Sean 0 1 2
John 1 0 0
My current code is:
SELECT b.username , COUNT(a.status_id)
FROM masterdb.auth_user b
left outer join masterdb.xmlform_joblist a
on a.user1_id = b.id
GROUP BY b.username, b.id, a.status_id
This gives me the separate count but in a single row without mentioning which status_id each column represents
This is called pivot and it works in two steps:
extracts the data for the specific field using a CASE statement
aggregates the data on users, to make every field value lie on the same record for each user
SELECT Username,
SUM(CASE WHEN status_id = 1 THEN 1 END) AS status_id_1,
SUM(CASE WHEN status_id = 2 THEN 1 END) AS status_id_2,
SUM(CASE WHEN status_id = 3 THEN 1 END) AS status_id_3
FROM t2
INNER JOIN t1
ON t2.user_id = t1._ID
GROUP BY Username
ORDER BY Username
Check the demo here.
Note: This solution assumes that there are 3 status_id values. If you need to generalize on the amount of status ids, you would require a dynamic query. In any case, it's better to avoid dynamic queries if you can.

SQL query inside another query?

I have this table:
total
user
wallet
storagesummary
chain
40
user1
wallet1
2
1
30
user1
wallet1
4
1
8
user1
wallet2
1
1
2
user2
wallet3
3
1
41
user2
wallet3
4
3
And this is what I'm trying to accomplish:
Type1, Type2, n... columns are a combination of storagesummary and chain
Just for the sake of example: if storagesummary == 2 and chain == 1, then it's gonna be Type1.
I tried something like this, but I don't think it's the best approach and I dunno what to do with the user/wallet columns:
SELECT (SELECT total from MyTable where storagesummary = 2 and chain == 1) as Total Type1 Count,
(SELECT total from MyTable where storagesummary = 4 and chain == 1) as Total Type2 Count,
.......
I cant' figure out how to achieve this.
This could use a bit of conditional aggregation to pivot that.
SELECT
CONCAT(t.user, ' / ', t.wallet) AS "User Wallet Address"
, SUM(CASE
WHEN t.storagesummary = 2 AND t.chain = 1
THEN t.total
ELSE 0
END) AS "Total Type1"
, SUM(CASE
WHEN t.storagesummary = 4 AND t.chain = 3
THEN t.total
WHEN t.storagesummary = 1 AND t.chain = 1
THEN t.total
ELSE 0
END) AS "Total Type2"
, SUM(CASE
WHEN t.storagesummary IN(2,5) AND t.chain >= 2
THEN t.total
ELSE 0
END) AS "Total Type3"
FROM MyTable t
GROUP BY t.user, t.wallet
ORDER BY t.user, t.wallet
Not sure if you need SUM or MAX though.
And the logic in the CASE's will needs corrections.
But if you get the concept, then you'll understand what to change.
I think what you are missing is matching internal column names with outer select, for example:
select action_id, next_action_id from ( select action_id, lead(action_id) over (order by action_id) next_action_id from action) where action_id <> next_action_id-1;
that's for oracle db, but normal SQL can fit without next/lead thing.

How to check the value of any row in a group after a previous one fulfils a condition?

I have a dataset grouped by test subjects that is filled according to the actions they perform. I need to find which customer does A and then, at some point, does B; but it doesn't necessarily have to be in the next action/row. And it can't be first does B and then A, it has to be specifically in that order. For example, I have this table:
Subject ActionID ActionOrder
1 A 1
1 C 2
1 D 3
1 B 4
1 C 5
2 D 1
2 A 2
2 C 3
2 B 4
3 B 1
3 D 2
3 A 3
4 A 1
Here subjects 1 and 2 are the ones that fulfil the order of actions condition. While 3 does not because it performs the actions in reverse order. And 4 only does action A
How can I get only subjects 1 and 2 as results? Thank you very much
Use conditional aggregation:
SELECT Subject
FROM tablename
WHERE ActionID IN ('A', 'B')
GROUP BY Subject
HAVING MAX(CASE WHEN ActionID = 'A' THEN ActionOrder END) <
MIN(CASE WHEN ActionID = 'B' THEN ActionOrder END)
See the demo.
Consider below option
select Subject
from (
select Subject,
regexp_replace(string_agg(ActionID, '' order by ActionOrder), r'[^AB]', '') check
from `project.dataset.table`
group by Subject
)
where not starts_with(check, 'B')
and check like '%AB%'
Above assumes that Subject can potentially do same actions multiple times that's why few extra checks in where clause. Other wise it would be just check = 'AB'

Select groups given a condition in a variable sql

I must do a query where I select those groups, given by de concatenation between sample and serial that could be defined as household, where at least one in the variable bplcountry = 1
sample serial bplcountry
1 1 2
1 1 1
1 3 2
2 1 2
2 2 2
2 3 2
3 1 2
3 3 2
3 3 1
I have made some research but I'm very amateur on SQL. I get some hint like this:
SELECT *
FROM latinCensus
GROUP BY sample AND serial
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
Also I got some idea in this way
SELECT *
FROM latinCensus
GROUP BY CONCAT(sample,serial)
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
I would expect something like this:
sample serial bplcountry
1 1 2
1 1 1
3 3 2
3 3 1
I will appreciate your help!
You want the pairs where bplcountry is 1. You can use window functions:
select lc.*
from (select lc.*,
sum(case when bplcountry = 1 then 1 else 0 end) over (partition by sample, serial) as cnt_1
from latincensus lc
) lc
where cnt_1 > 0;
Or use exists:
select lc.*
from latincensus lc
where exists (select 1
from latincensus lc2
where lc2.sample = lc.sample and lc2.serial = lc.serial and
lc2.bplcountry = 1
);
You haven't tagged your db, but something along these lines should work (can also be expressed using joins)
select sample, serial, bplcountry
from t
where (sample,serial) in (select sample,serial
from t
where bplcountry=1);

Enquiry on Query in Oracle SQL

I have data as below:
Category | Type | Rank
Milk 1 1
Milk 2 2
Milk 3 3
Chocolate 1 2
Candy 1 1
Any idea to achieve the output of below with a flat SQL query:
Category
Milk
Query must satisfy the below conditions:
1. Only Type 1 and Rank 1 will be selected.
2. Only Category that has Type 1 and Type 2 will be selected.
In the sample data above, only Milk that satisfy the conditions mentioned above.
My query is below. But it's incorrect, because it will return Candy as well.
SELECT DISTINCT Category
FROM table
WHERE Type = 1 AND rank = 1
Thanks in advance!
You can try below -
DEMO
select distinct category
from table a
WHERE Type = 1 AND rank = 1
and exists
(select 1 from table b where a.category=b.category and type in (1,2)
group by category having count(distinct type)=2)
OUTPUT:
category
Milk
You can use aggregation:
select category
from t
group by category
having sum(case when type = 1 and rank = 1 then 1 else 0 end) > 0 and
sum(case when type = 2 then 1 else 0 end) > 0;
Assuming no duplicates, this can be simplified to:
select category
from t
where (type = 1 and rank = 1) or type = 2
group by category
having count(distinct type) = 2;