How to create cases for multiple purposes and just showing the count of unique IDs for multiple cases - sql

I wrote a CTE which helps determine a flag based off a certain client's ID and what kind of client they are. I am looking to test the counts of the Flags, and the counts are totaling out amazingly! However, I am looking to add additional columns to the very last section of my code to show the amount of IDs who have belonged in all 3 cases, or both PPP and R, PPP and RR, RR and PPP, or RR and R. Is there a way I could do this? I know SUM won't work. I'm thinking using CASE or an IF, however I am a novice to SQL and am unsure what to do.
WITH ids AS (
SELECT DISTINCT LOWER(r.entry_id) AS ID
FROM id_user AS r
UNION
SELECT DISTINCT LOWER(identifiervalue) AS ID
FROM account AS a
)
,PPP as (
SELECT DISTINCT
LOWER(accountid) as "ID"
FROM ppp
WHERE (date >= '2022-11-21')
)
,R as (
SELECT DISTINCT
LOWER(account_id) as "ID"
FROM user
)
, RR as (
SELECT DISTINCT
LOWER(id) AS "ID"
FROM program_member
)
, Joint as (
SELECT
r.ID
,CASE WHEN p.ID IS NULL THEN 0 ELSE 1 END AS "PPP Flag"
,CASE WHEN r.ID IS NULL THEN 0 ELSE 1 END AS "R Flag"
,CASE WHEN rr.raid IS NULL THEN 0 ELSE 1 END AS "RR Flag"
FROM ids i
LEFT JOIN PPP ppp ON i.RAID = ppp.RAID
LEFT JOIN R r ON i.RAID = r.RAID
LEFT JOIN RR rr on i.RAID = rr.RAID
----TESTING COUNTS
SELECT
COUNT(ID) AS "ID Count"
,sum("PPP Flag") AS "PPP Users"
,sum("R") AS "R Accounts"
,sum("RR Flag") AS "RR Users"
FROM Joint

if all you are trying to do is get distinct counts, and how many are within each of those given flag categories, dont try to repeat then join. They are either in the PPP, User or Program_Member table. Just get that
select
UnionCnt.IDAndRaidCnt,
P.PPPCnt,
R.RCnt,
RR.RRCnt
from
-- this outer query returns only 1 record
( select
count(*) IDAndRaidCnt
from
-- this inner query returns all distinct based on the UNION result
(SELECT DISTINCT
LOWER(r.entry_id) ID
FROM
id_user r
UNION
SELECT DISTINCT
LOWER(identifiervalue) ID
FROM
account a)
) UnionCnt
JOIN
(SELECT
count( DISTINCT LOWER(accountid)) PPPCnt
FROM
ppp
WHERE
date >= '2022-11-21' ) P
-- above will only return a single record anyhow
on 1=1
JOIN
(SELECT
count( DISTINCT LOWER(account_id) RCnt
FROM
user ) R
-- also returns single row with total qualifying distinct ID count
on 1=1
JOIN
(SELECT
count( DISTINCT LOWER(id)) RRCnt
FROM
program_member ) RR
-- same here, single record
on 1=1

Related

SQL Query: Count "id" occurrences in two tables

I have these 3 tables and I am trying to count, how many "hints" and "quizzes" are there for specific town id.
db_town
id
town
1
New York
db_hint
id
town_id
hint
1
1
test
db_quiz
id
town_id
quiz
1
1
quiz 1
2
1
quiz 2
I am using this statement, but it does not work :(
SELECT count(q.id),count(h.id) FROM `db_town` t LEFT JOIN `db_quiz` q ON t.id = q.town_id LEFT JOIN `db_hint` h ON t.id = h.town_id WHERE t.id = 1 GROUP BY t.id
and it produces this result:
count(q.id)
count(h.id)
2
2
Do I need to use two statements? Or is it possible to query it in a single SQL statement? I am using MariaDB.
You can use union all and aggregation:
select town_id, sum(is_hint), sum(is_quiz)
from ((select town_id, 1 as is_hint, 0 as is_quiz
from hints
) union all
(select town_id, 0, 1
from quizzes
)
) t
group by town_id;
Alternatively, you can use correlated subqueries:
select t.*,
(select count(*) from hints h where h.town_id = t.id),
(select count(*) from quizzes q where q.town_id = t.id)
from towns t;
Two things to look out for:
JOINs are likely to multiply rows and throw off the counts.
Getting 0 values if a town has no hints or quizzes.
You can use COUNT (DISTINCT) if both the hint id and the quiz id are unique.
SELECT
count(distinct q.id),count(distinct h.id)
FROM `db_town` t
LEFT JOIN `db_quiz` q ON t.id = q.town_id
LEFT JOIN `db_hint` h ON t.id = h.town_id
WHERE t.id = 1 GROUP BY t.id

Checking 2 tables for identical IDs and returning 1/0 in column if ID in table 1 exists in table 2

I have 2 tables, 1 containing all reservation ids, and 1 containing reservation ids for livestream reservations. I am trying to write a query that checks to see if a reservation id exists in the livestream table, and returns '1' if true & '0' if false. I figure the best way to do this is with a case statement that returns my result if the reservation id exists in the livestream table, but I am running into issues. Is there a better way to do this?
with table_name as(
select
reservation_id
from all_reservations
)
select t.*,
case when exists(l.reservation_id)
then '1'
else '0' end as is_livestream
from livestream_reservations l
left join table name t
on l.reservation_id = t.reservation_id
So long as reservation_id shows up with at most one record in livestream_reservations, this will work for you:
select r.*,
case
when l.reservation_id is null then 0
else 1
end as is_livestream
from reservations r
left join livestream_reservations l
on l.reservation_id = r.reservation_id;
The case relies on the fact that a failure to join to livestream_reservations returns null in all columns from that table.
In case there may be more than one row with the same reservation_id in the livestream_reservations table, then you could do this:
with ls_count as (
select reservation_id, count(*) as count_livestream
from livestream_reservations
group by reservation_id
)
select r.*, coalesce(lc.count_livestream, 0) as count_livestream
from reservations r
left join ls_count lc on lc.reservation_id = r.reservation_id;
I would recommend exists and using booleans:
select r.*,
(exists (select 1 from livestream_reservations lr where lr.reservation_id = r. reservation_id)
) as is_livestream
from reservations r;
There is a good chance that this is faster than other solutions. More importantly, it avoids problems with duplicates in livestream_reservations.

Condition on count of associated records in SQL

I have the following tables (with given columns):
houses (id)
users (id, house_id, active)
custom_values (name, house_id, type)
I want to get all the (distinct) houses and the count of associated users that:
have at least 1 associated custom_value which name column contains the string 'red' (case insensitive) AND the custom_value column type value is 'mandatory'.
have at least 100 associated users which status column is 'active'
How can I run this query in PostgreSQL?
Right now I have this query (which was answered in Get records where associated records name contain a string AND associated record count is bigger than threshold), but I don't know how to select the count of users too (:
select h.*
from houses
where
exists (
select 1
from custom_values cv
where cv.house_id = h.house_id and cv.type = 'mandatory' and lower(cv.name) = 'red'
)
and (
select count(*)
from users u
where u.house_id = h.house_id and u.status = 'active'
) >= 100
You can turn the subquery to a lateral join:
select h.*, u.no_users
from houses h
cross join lateral (
select count(*) no_users
from users u
where u.house_id = h.house_id and u.status = 'active'
) u
where
u.cnt >= 100
and exists (
select 1
from custom_values cv
where cv.house_id = h.house_id and cv.type = 'mandatory' and lower(cv.name) = 'red'
)

How to group results by count of relationships

Given tables, Profiles, and Memberships where a profile has many memberships, how do I query profiles based on the number of memberships?
For example I want to get the number of profiles with 2 memberships. I can get the number of profiles for each membership with:
SELECT "memberships"."profile_id", COUNT("profiles"."id") AS "membership_count"
FROM "profiles"
INNER JOIN "memberships" on "profiles"."id" = "memberships"."profile_id"
GROUP BY "memberships"."profile_id"
That returns results like
profile_id | membership_count
_____________________________
1 2
2 5
3 2
...
But how do I group and sum the counts to get the query to return results like:
n | profiles_with_n_memberships
_____________________________
1 36
2 28
3 29
...
Or even just a query for a single value of n that would return
profiles_with_2_memberships
___________________________
28
I don't have your sample data, but I just recreated the scenario here with a single table : Demo
You could LEFT JOIN the counts with generate_series() and get zeroes for missing count of n memberships. If you don't want zeros, just use the second query.
Query1
WITH c
AS (
SELECT profile_id
,count(*) ct
FROM Table1
GROUP BY profile_id
)
,m
AS (
SELECT MAX(ct) AS max_ct
FROM c
)
SELECT n
,COUNT(c.profile_id)
FROM m
CROSS JOIN generate_series(1, m.max_ct) AS i(n)
LEFT JOIN c ON c.ct = i.n
GROUP BY n
ORDER BY n;
Query2
WITH c
AS (
SELECT profile_id
,count(*) ct
FROM Table1
GROUP BY profile_id
)
SELECT ct
,COUNT(*)
FROM c
GROUP BY ct
ORDER BY ct;

I need a SQL query for comparing column values against rows in the same table

I have a table called BB_BOATBKG which holds passengers travel details with columns Z_ID, BK_KEY and PAXSUM where:
Z_ID = BookingNumber* LegNumber
BK_KEY = BookingNumber
PAXSUM = Total number passengers travelled in each leg for a particular booking
For Example:
Z_ID BK_KEY PAXSUM
001234*01 001234 2
001234*02 001234 3
001287*01 001287 5
001287*02 001287 5
002323*01 002323 7
002323*02 002323 6
I would like to get a list of all Booking Numbers BK_KEY from BB_BOATBKG where the total number of passengers PAXSUM is different in each leg for the same booking
Example, For Booking number A, A*Leg01 might have 2 Passengers, A* Leg02 might have 3 passengers
Dependent of your RDBMs there might be several options availible. A solution that should work for most is:
SELECT A.Z_ID, A.BK_KEY, A.PAXSUM
FROM BB_BOATBKG A
JOIN (
SELECT BK_KEY
FBB_BOATBKGROM BB_BBK_KEY
GROUP BY BK_KEY
HAVING COUNT( DISTINCT PAXSUM ) > 1
) B
ON A.BK_KEY = B.BK_KEY
If your DBMS support OLAP functions, have a look at RANK() OVER (...)
It's a little counterintuitive, but you could join the table to itself on {BK_KEY, PAXSUM} and pull out only the records whose joined result is null.
I think this does it:
SELECT
a.BK_KEY
FROM
BB_BOATBKG a
LEFT OUTER JOIN BB_BOATBKG b ON a.BK_KEY = b.BK_KEY AND a.PAXSUM = b.PAXSUM
WHERE
b.Z_ID IS NULL
GROUP BY
a.BK_KEY
Edit: I think I missed anything beyond the trivial case. I think you can do it with some really nasty subselecting though, a la:
SELECT
b.BK_KEY
FROM
(
SELECT
a.BK_KEY,
Count = COUNT(*)
FROM
(
SELECT
a.BK_KEY,
a.PAXSUM
FROM
BB_BOATBKG a
GROUP BY
a.BK_KEY,
a.PAXSUM
HAVING
COUNT(*) = 1
) a
GROUP BY
a.BK_KEY
) b
INNER JOIN
(
SELECT
c.BK_KEY,
Count = COUNT(*)
FROM
BB_BOATBKG c
GROUP BY
c.BK_KEY
) c ON b.BK_KEY = c.BK_KEY AND b.Count = c.Count