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

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'

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.

Group Matching Values and Finding Which Ones Are Missing an Associated Value?

I need help with writing a query to generate a result that will provide me with all record numbers NOT assigned to Group D from a table structured like the below example. From the below table my desired result would be record number "3" .
Record_Number Assigned_To_Group
1 A
1 B
1 C
1 D
2 A
2 E
2 D
3 A
3 B
3 E
One method uses aggregation:
select Record_Number
from t
group by Record_Number
having sum(case when Assigned_To_Group = 'D' then 1 else 0 end) = 0;

Best way to by column and aggregation on another column

I want to create a rank column using existing rank and binary columns. Suppose for example a table with ID, RISK, CONTACT, DATE. The existing rank is RISK, say 1,2,3,NULL, with 3 being the highest. The binary-valued is CONTACT with 0,1 or FAILURE/SUCESS. I want to create a new RANK that will order by RISK once a certain number of successful contacts has been exceeded.
For example, suppose the constraint is a minimum of 2 successful contacts. Then the rank should be created as follows in the two instances below:
Instance 1. Three ID, all have a min of two successful contacts. In that case the rank mirrors the risk:
ID risk contact date rank
1 3 S 1 3
1 3 S 2 3
1 3 F 3 3
1 3 F 4 3
2 2 S 1 2
2 2 S 2 2
2 2 F 3 2
2 2 F 4 2
3 1 S 1 1
3 1 S 2 1
3 1 S 3 1
Instance 2. Suppose ID=1 has only one successful contact. In that case it is relegated to the lowest rank, rank=1, while ID=2 gets the highest value, rank=3, and ID=3 maps to rank=2 because it satisfies the constraint but has a lower risk value than ID=2:
ID risk contact date rank
1 3 S 1 1
1 3 F 2 1
1 3 F 3 1
1 3 F 4 1
2 2 S 1 3
2 2 S 2 3
2 2 F 3 3
2 2 F 4 3
3 1 S 1 2
3 1 S 2 2
3 1 S 3 2
This is SQL, specifically Hive. Thanks in advance.
Edit - I think Gordon Linoff's code does it correctly. In the end, I used three interim tables. The code looks like that:
First,
--numerize risk, contact
select A.* ,
case when A.risk = 'H' then 3
when A.risk = 'M' then 2
when A.risk = 'L' then 1
when A.risk is NULL then NULL
when A.risk = 'NULL' then NULL
else -999 end as RISK_RANK,
case when A.contact = 'Successful' then 1
else NULL end as success
Second,
-- sum_successes_by_risk
select A.* ,
B.sum_successes_by_risk
from T as A
inner join
(select A.person, A.program, A.risk, sum(a.success) as sum_successes_by_risk
from T as A
group by A.person, A.program, A.risk
) as B
on A.program = B.program
and A.person = B.person
and A.risk = B.risk
Third,
--Create table that contains only max risk category
select A.* ,
B.max_risk_rank
from T as A
inner join
(select A.person, max(A.risk_rank) as max_risk_rank
from T as A
group by A.person
) as B
on A.person = B.person
and A.risk_rank = B.max_risk_rank
This is hard to follow, but I think you just want window functions:
select t.*,
(case when sum(case when contact = 'S' then 1 else 0 end) over (partition by id) >= 2
then risk
else 1
end) as new_risk
from t;

Creating a select to 'de-normalise' the data within a table

I put the work de-normalise in quote marks, because it might not be the right way of putting it, but not too sure how else to describe it...
I have the following table
Source Priority Attribute
A 1 Name
B 2 Name
C 3 Name
A 1 Address
B 2 Address
C 3 Address
A 2 Email
B 3 Email
C 1 Email
I would like my select to return:
Source Name_Pri Addr_Pri Email_Pri
A 1 1 2
B 2 2 3
C 3 3 1
Thanks
You are looking for a pivot. I often do this using conditional aggregation:
select source,
max(case when attribute = 'Name' then priority end) as name_priority,
max(case when attribute = 'Address' then priority end) as address_priority,
max(case when attribute = 'Email' then priority end) as email_priority
from t
group by source;

SQL: A count inside a case inside a case perhaps?

Good day all.
below is an image relating to what I am attempting to achieve.
In one table there is two fields one is an ID and one is a Type.
I figured a picture paints a thousand words, so check the below
I have tried a few things with case and other things but none worked.
There is a couple of things to note: We cannot use temporary tables, inserts or deletes due to certain limitations.
Data Sample:
ID Type
3 bad
2 zeal
4 tro
3 pol
2 tro
2 lata
4 wrong
3 dead
2 wrong
3 dead
4 wrong
3 lata
2 bad
2 zeal
First of all you need a table containing the type groups:
type typegroup
bad 1
tro 1
zeal 1
dead 2
lata 2
wrong 2
pol 3
Then join, group by type group in order to get one result line per type group and count.
select
tg.typegroup,
count(case when id = 2 then 1 end) as id2,
count(case when id = 3 then 1 end) as id3
count(case when id = 4 then 1 end) as id4
from typegroups tg
join mytable m on m.type = tg.type
group by tg.typegroup
order by tg.typegroup;
UPDATE: Of course you can create such table on-the-fly.
...
from
(
select 'bad' as type, 1 as typegroup
union all
select 'tro' as type, 1 as typegroup
union all
...
) tg
join mytable m on m.type = tg.type
...
And you can move this to a WITH clause if you prefer so.