mysql views my query output i need different manner - mysql

This is my query
Select Count(Case When campaign_pid != 0 Then 1 End) As Email,
Count(Case When sms_pid != 0 Then 1 End) As Sms,
Count(Case When survey_pid != 0 Then 1 End) As Survey
From tablename
Please help me
This is my query output
Email Sms Survey
21 1 4
i need output like this
name value
Email 21
Sms 1
Survey 4
Please tell me how to get like above optput

TRY
SELECT COUNT(Case WHEN campaign_pid != 0 THEN 1 END) AS Email FROM tablename
UNION
SELECT COUNT(Case WHEN sms_pid != 0 THEN 1 END) As Sms FROM tablename
UNION
SELECT COUNT(Case WHEN survey_pid != 0 THEN 1 END) As Survey FROM tablename
Reference

Related

Count data with mysql query

i have this following table
id kelurahan status
1 Pegambiran Netral
2 Pegambiran Netral
3 Kejaksan Positif
4 Kesenden Positif
5 Pegambiran Negatif
i want to get result like this
kelurahan count_positif count_netral count_negatif total
Pegambiran 0 2 1 3
Kejaksan 1 0 0 1
Kesenden 1 0 0 1
i tried this query
SELECT kelurahan,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Positif' GROUP BY kelurahan LIMIT 1) AS count_positif,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Netral' GROUP BY kelurahan) AS count_netral,
(SELECT COUNT(status) FROM tbl_monitoring WHERE status = 'Negatif' GROUP BY kelurahan) AS count_negatif,
COUNT(kelurahan) AS total
FROM tbl_monitoring GROUP BY kelurahan
i get the result like this
any help would be appreciated, thanks.
You should use SUM, not COUNT.
Tested on dbfiddle
SELECT
kelurahan,
SUM(CASE WHEN status = 'Positif' THEN 1 ELSE 0 END) AS count_positif,
SUM(CASE WHEN status = 'Netral' THEN 1 ELSE 0 END) AS count_netral,
SUM(CASE WHEN status = 'Negatif' THEN 1 ELSE 0 END) AS count_negatif,
SUM(1) AS total
FROM tbl_monitoring
GROUP BY kelurahan;

MySQL- Count total records based on different values of same column

I have a table as follows
table_user
id name status flag country
====================================================================================================
1 AB 1 0 US
2 BC 1 0 UK
3 CD 0 0 IN
4 DE 3 0 BR
5 EF 3 0 UK
6 FG 2 0 IN
7 GH 4 0 IN
I want to count the no. of records where
status = 1 as totalactiverecords,
status = 0 as totalinactiverecords,
status = 3 as totalrecentactiverecords,
status = 2 as totalemailnotverifiedrecords,
status = 4 as totalemailverifiedrecords,
, and country is UK, all in a single SELECT statement.
Is there any specific way to do it?
I have thought of something like
SELECT COUNT(*) as totalactiverecords WHERE status=1
COUNT(*) as totalinactiverecordsWHERE status=0,
COUNT(*) as totalemailnotverifiedrecords WHERE status=2,
COUNT(*) as totalrecentactiverecords WHERE status=3,
COUNT(*) as totalemailverifiedrecords WHERE status=4
FROM table_user where country=IN
,but that does not seem to work.
You can try this query:
SELECT count(case status when '1' then 1 else null end) as totalactiverecords,
count(case status when '0' then 1 else null end) as totalinactiverecords,
count(case status when '2' then 1 else null end) as totalemailnotverifiedrecords,
count(case status when '3' then 1 else null end) as totalrecentactiverecords,
count(case status when '4' then 1 else null end) as totalemailverifiedrecords
FROM table_user where country='IN'
I usually use CASE WHEN in this kind of problem:
SELECT sum(case when status = 1 then 1 end) as totalactiverecords,
COUNT sum(case when status = 0 then 1 end) as totalinactiverecords,
COUNT sum(case when status = 2 then 1 end) as totalemailnotverifiedrecord,
COUNT sum(case when status = 3 then 1 end) as totalrecentactiverecords,
COUNT sum(case when status = 4 then 1 end) as totalemailverifiedrecords
FROM table_user where country=IN

Multiple Count with Multiple column

I am new in sql. I want to count something like:
Select count(*) from table where col1= x and col2=x and Col3=x.
I need to count the same value in all different column.
Any help will be appreciated.
You can use conditional aggregation :
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab;
If you want to have sum of these count values, consider the above query as an inner and use the following :
Select q.*,
q.count_col1 + q.count_col2 + q.count_col3 whole_sum
from
(
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab
) q
Rextester Demo

Is there a way to include the sum of all cases?

Given SQL: Is there a way to bring in the total of the result set?
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE
id = 2;
Result:
Open,Close
5,5
Desired Result:
Open,Close,Total
5,5,10
just add another case statement
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
SUM(CASE
WHEN status IN (3, 4) THEN 1
ELSE 0
END) AS Total
FROM
Table1
WHERE
id = 2;
You can use a CTE:
WITH sumCase AS (
SELECT
SUM(CASE
WHEN status = 3 THEN 1
ELSE 0
END) AS Open,
SUM(CASE
WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE
id = 2;)
SELECT Open,Close, Open + Close AS Total FROM Table1;
http://www.mysqltutorial.org/mysql-cte/
using sub-query
select open,close,open+close as total from
(
SELECT
SUM(CASE WHEN status = 3 THEN 1
ELSE 0
END ) AS Open,
SUM(CASE WHEN status = 4 THEN 1
ELSE 0
END) AS Close
FROM
Table1
WHERE id = 2 ) as T

mysql views my query output i need different manner

This is my query
Select Count(Case When campaign_pid != 0 Then 1 End)
As Email, Count(Case When sms_pid != 0 Then 1 End)
As Sms, Count(Case When survey_pid != 0 Then 1 End)
As Survey From tablename
Please help me This is my query output
Email Sms Survey
21 1 4
i need output like this
name value
Email 21
Sms 1
Survey 4
Please tell me how to get like above optput
Something like the following should work
SELECT 'Email' AS Name, Count(Case When campaign_pid != 0 Then 1 End) As Value From tablename
UNION SELECT 'SMS', Count(Case When sms_pid != 0 Then 1 End)s From tablename
UNION SELECT 'Survey', Count(Case When survey_pid != 0 Then 1 End) From tablename
or better still
SELECT 'Email' AS Name, COUNT() AS Value FROM tablename WHERE campaign_pid <> 0
UNION SELECT 'SMS', COUNT() FROM tablename WHERE sms_pid <> 0
UNION SELECT 'Survey', COUNT() FROM tablename WHERE survey_pid <> 0
Not 100% sure of your additional question, maybe this is what you want
SELECT COUNT(DISTINCT some_unique_col)
FROM table_name
WHERE campaign_pid <> 0
AND sms_pid <> 0
AND survey_pid <> 0
Maybe you want SUM instead of COUNT, if you could provide some sample data and expected results from that it would help