How can I total up 3 select statement count? - mysql

I have three select statements as follows and I would like to sum up total number of records. How can I do that?
SELECT COUNT(*) AS Number FROM tableA where user_id = 5 //Total 5 records
SELECT COUNT(*) AS Number FROM tableB where user_id = 5 //Total 6 records
SELECT COUNT(*) AS Number FROM tableC where user_id = 5 //Total 1 records
so return result will be 12.

You could apply count(*) to the result of a union all:
SELECT COUNT(*)
FROM (SELECT user_id FROM tablea
UNION ALL
SELECT user_id FROM tableb
UNION ALL
SELECT user_id FROM tablec) t
WHERE user_id = 5

I think this should do the trick:
SELECT SUM(Number)
FROM (
SELECT COUNT(*) AS Number FROM tableA where user_id = 5
UNION ALL
SELECT COUNT(*) AS Number FROM tableB where user_id = 5
UNION ALL
SELECT COUNT(*) AS Number FROM tableC where user_id = 5
)

select ( select count(*) from tableA where user_id = 5 )
+ ( select count(*) from tableB where user_id = 5 )
+ ( select count(*) from tableC where user_id = 5 )
as total_rows
from dual

Related

SQL count of distinct union

I have a query that captures customer ids from three tables (each table is a different contact method).
I want to get the count of distinct customer ids after the unions.
The SQL statement below is working and returns a list of unique customer ids (no dups):
SELECT DISTINCT customer_id
FROM email_contact
WHERE info_id = 1
AND status = 'SENT'
UNION
SELECT DISTINCT customer_id
FROM call_contact
WHERE info_id = 1
AND status = 'CALLED'
UNION
SELECT DISTINCT customer_id
FROM mail_contact
WHERE info_id = 1
AND status = 'MAILED'
From that query I want a count of customers, but my attempts to wrap the query in a select count keep producing syntax errors. How can wrap the unions to provide me with a count of the clients?
I would recommend:
SELECT COUNT(DISTINCT customer_id)
FROM (
SELECT customer_id FROM email_contact WHERE info_id = 1 AND status = 'SENT'
UNION ALL SELECT customer_id FROM call_contact WHERE info_id = 1 AND status = 'CALLED'
UNION ALL SELECT customer_id FROM mail_contact WHERE info_id = 1 AND status = 'MAILED'
) t
I removed the DISTINCT and I changed the UNIONs to UNION ALL, so the database just gathers all the rows from the 3 union members without attempting to manage duplicates (this is fast). Then, you can use COUNT(DISTINCT ...) in the outer query.
You can wrap it like this
SELECT COUNT(*)
FROM
( SELECT DISTINCT customer_id
FROM email_contact
WHERE info_id = 1
AND status = 'SENT'
UNION
SELECT DISTINCT customer_id
FROM call_contact
WHERE info_id = 1
AND status = 'CALLED'
UNION
SELECT DISTINCT customer_id
FROM mail_contact
WHERE info_id = 1
AND status = 'MAILED') t1
Is this what you ant?
SELECT COUNT(*)
FROM ((SELECT customer_id
FROM email_contact
WHERE info_id = 1 AND status = 'SENT'
) UNION -- on purpose to remove duplicates
(SELECT customer_id
FROM call_contact
WHERE info_id = 1 AND status = 'CALLED'
) UNION
(SELECT customer_id
FROM mail_contact
WHERE info_id = 1 AND status = 'MAILED'
)
) c;
Note that all your DISTINCTs are unnecessary because UNION removes duplicates.

Get count with having particular value associated with unique id

I Have below mentioned table:
ID Value
U-1 ACB
U-1 ART
U-1 DDD
U-2 ACB
U-2 DDD
U-3 XCC
U-3 DFC
I want to fetch those rows where Value is DDD but total count of unique ID is <3.
Required Output:
ID Value
U-2 ACB
U-2 DDD
You could use a self join to same table, Inner query will calculate count per id and filter rows where count is less than 3
select a.*
from table1 a
join (
select id, count(*) total
from table1
group by id
having total < 3
and sum(`Value` = 'DDD') > 0
) t using(id);
Demo
OR
select a.*
from table1 a
where (
select count(*)
from table1
where ID = a.ID
having sum(`Value` = 'DDD') > 0
) < 3
but i prefer join approach
updated demo
How about this?
SELECT * FROM
(SELECT * FROM sof t1
WHERE (SELECT COUNT(id) FROM sof t2 WHERE t2.id = t1.id) < 3) as temp2
WHERE id IN (SELECT id FROM sof WHERE value = 'DDD')
The input and output matches for your case at my end atleast.
Demo: http://rextester.com/AZLA7822

UNION All, Group BY and then get overall COUNT

I have 2 Tables..
User 1
user_id mobile_no
1 1111
2 2222
User 2
user_id mobile_no
1 3333
2 2222
I Want to first UNION These tables, then group by and then want to count total records
I am using this query but it's not working
SELECT COUNT(Q2.total) AS Overall
FROM (
SELECT COUNT(Q.user_id) AS total
FROM (
SELECT * FROM user1
UNION ALL
SELECT * FROM user2
) Q
GROUP BY Q.mobile_no
) Q2
if i user Inner Query e-g:
SELECT COUNT(Q.user_id) AS total
FROM (
SELECT * FROM user1
UNION ALL
SELECT * FROM user2
) Q
GROUP BY Q.mobile_no
I get these results, actually i want to again count these result...
total
2
1
1
i expect this result
Overall
3
This is weird. No one seems to have realised it's as simple as:
select count(*) overall
from (select mobile_no from user1 union select mobile_no from user2)
The difference between union and union all is that union removes duplicates.
Assuming that you are looking for the distinct number of mobile numbers:
select count(distinct mobile_no) as Overall
from (
select user_id, mobile_no
from user1
union all
select user_id, mobile_no
from user2
) a
select count (distinct mobile_no) from
(select user_id, mobile_no from user1 u1
UNION ALL
select user_id, mobile_no from user2 u2
) X
group by X.mobile_no
Rather use UNION and not UNION ALL
SQL UNION Operator
The UNION operator selects only distinct values by default. To allow
duplicate values, use the ALL keyword with UNION.
SELECT COUNT(mobile_no) Overall
FROM (
SELECT
mobile_no
FROM User1
UNION
SELECT
mobile_no
FROM User2
) Q
EDIT:
As #Bohemian correctly stated, no need for the distinct.
Try this:
SELECT COUNT(*) FROM
( (SELECT * FROM user1) UNION
(SELECT user_id as u1,mobile_no as m1
FROM user2) ) as a1 GROUP BY a1 .1

mysql Query , calculate percentage of three grades

I want to calculate percentage of three different grades A,B,C
1 product have A grade
5 products have B grade
3 products gave C grade
SQL - This Query return 3 rows
SELECT count(*) FROM table1 WHERE pid = '480' AND grade IN
( SELECT DISTINCT grade FROM table1 )
GROUP BY grade
------------------------
CCA_ST2 count(*)
------------------------
A 1
B 5
C 3
------------------------
How can i calculate percentages ?
( SELECT count(*) FROM table1 WHERE pid = '480' AND grade IN
( SELECT DISTINCT grade FROM table1 )
GROUP BY grade
)
DIVIDE BY
( SELECT COUNT(*) FROM table1 WHERE pid = '480' ) * 100
I tried with this query
SELECT grade,
( SELECT count(*) FROM table1 WHERE pid = '480' AND grade IN
( SELECT DISTINCT grade FROM table1 )
GROUP BY grade
)
/
( SELECT count(*) FROM table1 WHERE pid = '480' )
* 100
AS score
FROM table1 GROUP BY grade
I got this error . How can i solve this
subquery returns more than one row
i don't really want to create procedure for this , i know this can be done by query . But i'm feeling so lazy today.
You can't take the count from all groups and divide them by the total count, you have to divide each count by the total count:
SELECT
grade,
100 * count(*) / (SELECT count(*) FROM table1 WHERE pid = '480') AS score
FROM table1
WHERE pid = '480'
GROUP BY grade
ORDER BY grade

Selecting the frequency of a result that could appear in multiple columns (SQL)

I have a table with a list of names spread across five different columns. I'm trying to get the 6 most frequent distinct names. Each name will only appear in each record once. The five columns are name_1, name_2...name_5. And just for names sake call the table 'mytable'.
Any help would be much appreciated.
Here's one approach:
SELECT name, COUNT(1)
FROM ( SELECT name_1 AS name FROM mytable
UNION ALL SELECT name_2 AS name FROM mytable
UNION ALL SELECT name_3 AS name FROM mytable
UNION ALL SELECT name_4 AS name FROM mytable
UNION ALL SELECT name_5 AS name FROM mytable
) AS myunion
GROUP BY name
ORDER BY COUNT(1) DESC LIMIT 6
;
How many rows are there in the table?
try this:
SELECT iTable.iName, Count(iTable.iName) as TotalCount
FROM
(
SELECT DISTINCT name_1 as iName FROM myTable
UNION
SELECT DISTINCT name_2 as iName FROM myTable
UNION
SELECT DISTINCT name_3 as iName FROM myTable
UNION
SELECT DISTINCT name_4 as iName FROM myTable
UNION
SELECT DISTINCT name_5 as iName FROM myTable
) as iTable
GROUP BY iTable.iName
ORDER BY TotalCount DESC
LIMIT 6
You should be able to select all the names from each table and union the results together. Then you can count the number of times each name occurs.
select *
from
(
select name, count(*)
from (
select name from table1
union all
select name from table2
union all
select name from table3
union all
select name from table4
union all
select name from table5
)
group by name
order by count(*) desc
)
where rownum <= 6
UNION + subselect should work for you in this case.
SELECT name_1, COUNT(*) FROM (
SELECT name_1 FROM mytable
UNION ALL SELECT name_2 FROM mytable
UNION ALL SELECT name_3 FROM mytable
UNION ALL SELECT name_4 FROM mytable
UNION ALL SELECT name_5 FROM mytable
) AS names GROUP BY name_1 ORDER BY 2 DESC LIMIT 6;