Display multiple count values on joined tables - sql

I am trying to get a count of the userID's that exist in 2 tables,
I also have an inner join for the tables on the userID so that I can search for the count of one table, based off the BadgeID from the other table.
So far it returns the user count for one table, but I can't seem to figure out how to return a count for userID on both tables, Any suggestions?
SELECT DISTINCT live_event.usid, COUNT(1) AS membercount
FROM live_event
INNER JOIN member_badges ON live_event.usid = member_badges.usid
WHERE bdgid = 14

Not sure what you are looking for; but based on what I could understand, are sub-queries perhaps the route you are wanting to go?
select
(select count(*) from firsttable where userid = {userid})
as count1,
(select count(*) from secondtable where userid = {userid})
as count2
Where {userid} would be your bdgid.

Try to use union select to connect results from two tables
select useid, count(*) from (
select useid from firsttable
union select useid from second table ) group by useid

Try this:
SELECT
user_id,
COALESCE(MAX(
CASE
when tbl_name='live_event' then cnt_records
else null
END), 0) as cnt_live_event,
COALESCE(MAX(CASE
when tbl_name='member_badges' then cnt_records
else null
END), 0) as cnt_member_badges
FROM
(
SELECT
'live_event' AS tbl_name,
usid as user_id,
count(*) AS cnt_records
FROM live_event
GROUP BY 'live_event', usid
UNION
SELECT
'member_badges',
usid as user_id,
count(*)
FROM member_badges
GROUP BY 'member_badges', usid
) grouped_counts
GROUP BY user_id
ORDER BY user_id;
The above query would get you the user counts even if the usid is present in only one of the tables.

Related

Alternative way of full outer join

I am running this query
select * from
(select name, count(distinct id) as ids, date
from table1
group by name, date ) as tt
full outer join
(select st_name as name,count(distinct id) as ids, date
from table2
group by st_name, date) as ts
on tt.name= ts.name
and tt.ids = ts.ids
It runs successfully but I want to ask if there is an alternative more efficient way to run this query.
I assume that you want to get days when the two numbers are not the same (it seems like the most reasonable thing you want from such a query). So, this addresses that question.
FULL OUTER JOIN should be fine. But an alternative is to try UNION ALL and aggregation:
select name, sum(ids_1), sum(ids_2), date
from ((select name, count(distinct id) as ids_1, NULL as ids_2, date
from table1
group by name, date
)
union all
(select st_name as name, NULL, count(distinct id) as ids_2, date
from table2
group by st_name, date
)
)
group by name, date
having sum(ids_1) = sum(ids_2)

New table with row counts

I have 5 different tables stored in Hive, and I would like to know how to create a new table, called total_counts which has 5 columns each with the total row count from the individual tables. So, something like
My data is road flights for each year from 2015 to 2019, so I would like a table which just gives me the total number of accidents in each year.
I have tried variations of the following:
create table total_counts
as select COUNT(*)
from flights_2014 as "2014_count", flights_2015 as "2015_count;
I can get the counts for an individual year, but I can't seem to give the columns a heading, nor can I figure out how to do it for all my tables.
Thanks.
Calculate counts in sub-queries and do cross joins if you want to store data in columns
CREATE TABLE total_counts AS
SELECT 2015_count.cnt as 2015_count, 2016_count.cnt as 2016_count, ...
FROM (SELECT COUNT(*) cnt FROM flights_2015) AS 2015_count
CROSS JOIN
(SELECT COUNT(*) cnt FROM flights_2016) AS 2016_count
...
Or the same using UNION ALL + aggregation:
CREATE TABLE total_counts AS
SELECT max(case when yr=2015 then cnt else 0 end) 2015_count,
max(case when yr=2016 then cnt else 0 end) 2016_count,
...
FROM (
SELECT COUNT(*) cnt, 2015 yr FROM flights_2015
UNION ALL
SELECT COUNT(*) cnt, 2016 yr FROM flights_2016
...
) u
CREATE TABLE total_counts AS
SELECT
(SELECT COUNT(*) FROM flights_2015) AS 2015_count,
(SELECT COUNT(*) FROM flights_2016) AS 2016_count;
etc.

Select Person with only one value in the column

I can't correctly write a query.
I need to select a person who has only one value in the column. For example,
select * (
select PersonID, sum(TotalAmount)
from Table1
group by PersonID
HAVING sum(TotalAmount) = 0 )
where Group = A
It means that I would select all customers that belong to ONLY 'A' group...
Could someone help me?
If you want the persons with only one value, then having count(*) = 1 comes to mind:
select personid
from table1
group by personid
having count(*) = 1;

How to get a combined count on two tables for shared id

I have two transcript tables, A_Transcript and B_Transcript, and I want a query that will give me all the users who have more than 2 total transcripts (across both tables). Thanks!
Here's the query if I just had one table with data:
select user_Id, count(*) From A_Transcript
group by user_ID
having count(*) > 2;
You need to get the full list of userids from both tables before grouping, and you need UNION ALL to do a non-distinct union:
SELECT user_Id, count(*)
FROM
(
SELECT user_Id
FROM A_Transcript a
UNION ALL
select user_Id
FROM B_Transcript b
) combined
GROUP BY user_Id
HAVING COUNT(*) >2
You can just union the 2 queries together.
SELECT user_Id, count(*) FROM A_Transcript
GROUP BY user_ID
HAVING count(*) > 2
UNION
SELECT user_Id, count(*) FROM B_Transcript
GROUP BY user_ID
HAVING count(*) > 2

Find duplicate ID's with different fields

I have a table that contain UserID's and Departments. UserID's can belong to several departments so their combo makes it unique.
However I have been trying to query trying to find where the UserID belongs to either one of two departments (hr or customer).
SELECT UserId, Dept, COUNT(*) Total
FROM MyTable
GROUP BY UserID
HAVING COUNT(*) = 1
However this still brings back duplicates if a UserId has both departments I guess because the combo makes it a unique record.
What I get back is this
UserID | Department | Total
1 hr 1
2 customer 1
3 customer 1
1 customer 1
3 hr 1
But what I am trying to get back is this
UserID | Department | Total
2 customer 1
Where any instances of UserId belonging to both departments are not included only if they belong to one or the other.
This should do the job
select t1.UserId, Dept, t2.Total
FROM MyTable t1
INNER JOIN
(
SELECT UserId, COUNT(*) Total
FROM Table1
GROUP BY UserID
HAVING COUNT(*) = 1
) t2 on t1.UserId = t2.UserId
try
SELECT UserId, COUNT(distinct Dept) Total
FROM MyTable
GROUP BY UserID
HAVING COUNT(distinct Dept) = 1
You can try something like this
select UserId, Dept
FROM Mytable where UserId in
(
SELECT UserId, COUNT(*) Total
FROM MyTable
GROUP BY UserID
HAVING COUNT(*) = 1
)
If you add the Dept column in your Group by you will always retrieve all the combinations.
So you have to select all users with only one Dept and then retrieve the additional informations
There is maybe syntax errors because I am usually working with oracle but I think the concept is correct
This will select users in 'hr' that do not have id's in 'customer' and then users in 'customer' that do not have ids in 'hr'.
I didn't include count on purpose as it can always only be one.
SELECT * FROM [MyTable] T WHERE Department = 'hr' AND NOT EXISTS (SELECT 1 FROM [MyTable] WHERE [MyTable].UserID =T.UserID AND Department = 'customer') UNION
SELECT * FROM [MyTable] T WHERE Department = 'customer' AND NOT EXISTS (SELECT 1 FROM [MyTable] WHERE [MyTable].UserID =T.UserID AND Department = 'hr')
I do not think the above query will run as Dept is not in group by clause and also count comparsion cannot be in that way.
Coming to your issue:
Select userid, Dept
from #temp
where userid in (Select userid from (Select userid,count(*) 'C'
from #temp group by userid ) u where u.c=1)
I do not think you require count as it is always 1
It's a fake question because you can't use Dept in this query.
I'm don't understand why author doesn't explain us about any compiler warnings and tells us some results of this query.
but you can yse this way if you really want to get some result:
SELECT UserId, min(Dept) as Dept
FROM MyTable
GROUP BY UserID
HAVING COUNT(*) = 1
The problem is the COUNT (*) at the end.
Try this:
SELECT UserId, Dept, COUNT(*) Total
FROM MyTable
GROUP BY UserID
HAVING COUNT(UserId) = 1