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

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

Related

How to get count of columns using single queries

I have a table in my database from which i want to fetch data on the basis of count
my table name is syncbill and columns are Cancelled and Noofprints i am trying to write a query to fetch the count of cancelled and count of noofbills using single query
For cancelled i want to fetch count of cancelled where cancelled<>Y
for Noofprints i want to fetch count of noofprints where noofprints>1
I am using
select count(CANCELLED) as canceledbill,count(NOOFPRINTS) as duplicatebill
from syncbill where CANCELLED<>'Y' and NOOFPRINTS>1`
this query but it is giving me the same no of counts for both
any one out-here please guide me
you can use sum() with case expreseeion
SELECT
sum(CASE WHEN CANCELLED <> 'Y' THEN 1 else 0 END) AS canceledbill,
sum(CASE WHEN NOOFPRINTS > 1 THEN 1 else 0 END) AS duplicatebill
FROM table_name;
Use conditional aggregation:
SELECT
COUNT(CASE WHEN CANCELLED <> 'Y' THEN 1 END) AS cancelledbill,
COUNT(CASE WHEN NOOFPRINTS > 1 THEN 1 END) AS duplicatebill
FROM yourTable;
If you are actually using MySQL, then the above can be simplified to this:
SELECT
SUM(CANCELLED <> 'Y') AS cancelledbill,
SUM(NOOFPRINTS > 1) AS duplicatebill
FROM yourTable;
USE CASE WHEN Expression
select
sum(case when CANCELLED <> 'Y' then 1 else 0 end) AS cancelledbill,
sum(case when NOOFPRINTS > 1 then 1 else 0 end) AS duplicatebill
FROM tablename;

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

Count different values in a column, while doing total and group by different column values

We have a table with data from different nodes and one of the column will have status report as "compliant or non-compliant", sample data as below
I want to filter the table in such a way that if any of the checks on a node shows non compliant, it should be flagged as non-compliant and rest as compliant. Using below query i am able to do it
SELECT COUNT(*) AS total_nodes,
SUM(fully_compliant = 0) AS Non_compliant_nodes,
SUM(fully_compliant = 1) AS compliant_nodes
FROM (
SELECT Node, CASE WHEN SUM(Status = 'Compliant') = COUNT(*) THEN 1 ELSE 0 END AS fully_compliant
FROM your_table GROUP BY Node
)
Now, i want to group and split the result by dept as below, how can i achieve this
I think you're looking for this:
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
sum(case when 'Non-Compliant' then 1 else 0 end) as non_compliant_chk
from your_table
group by dept,
node
) v
group by dept;
With few modifications to what Brian suggested, I am able to get the desired result
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
COUNT(CASE WHEN Compliance-Status = 'Non-Compliant' THEN 1 END) 'non_compliant_chk'
from table WHERE DOR >= DATE(NOW()) - INTERVAL 7 DAY
group by Dept,
Node
) v
group by Dept;

how to get column count

i have a query like below
select project_task_id,
status_id,
sum(case when StatusID=1 then 1 else 0 end) as task_id=1,
sum(case whenStatusID=2 then 1 else 0 end) as task_id=2,
sum(case when StatusID=3 then 1 else 0 end) as task_id=3,
sum(case when StatusID=4 then 1 else 0 end) as task_id=4,
sum(case when StatusID=5 then 1 else 0 end) as task_id=5,
sum(case when StatusID=6 then 1 else 0 end) as task_id=6,
sum(case when StatusID=7 then 1 else 0 end) as task_id=7,
from"Projects".work_unit_status
group by project_task_id,status_id;
I'm getting the below attached output:
https://i.stack.imgur.com/1wfD1.png
and I want to get the below expected output:
https://i.stack.imgur.com/Zql9z.png
include zero's if the status_id is blank
please any one help on this
Try this: use the addition of all sum column
select project_task_id,status_id,
isnull(sum(case when StatusID=1 then 1 else 0 end),0)+
isnull(sum(case whenStatusID=2 then 1 else 0 end),0) +
isnullsum(case when StatusID=3 then 1 else 0 end),0) +
isnullsum(case when StatusID=4 then 1 else 0 end),0)+
isnullsum(case when StatusID=5 then 1 else 0 end),0) +
isnullsum(case when StatusID=6 then 1 else 0 end),0) +
isnullsum(case when StatusID=7 then 1 else 0 end),0) as count_status
from"Projects".work_unit_status group by project_task_id,status_id
use in with your case
with t1 as (
select project_task_id,
status_id,
sum(case when StatusID in (1,2,3,4,5,6,7) then 1 else 0)
as sum_s
from "Projects".work_unit_status
group by project_task_id,status_id
) ,
t2 as
(
select * from (
select 1 as statusid
union
select 2
union
select 3
union
select 4
union
select 5
union
select 6
union
select 7 ) t
) select t1.project_task_id,
t2.statusid,
case when t1.sum_s>0 or not null
then sum_s else 0 end as total
t2 full join t1 on t2.statusid=t1.status_id
Without knowing the exact table structure I assumed that status_id and statusId refer to the same column. (If they are different columns, we need to use StatusId in the COUNT.)
Based on the expected output, you want to count the status_id and group by project_task_id. To make sure that every status is represented for every task, first, we need to create a subquery of all possible project_task_id/status_id combinations. Then we use that with the aggregate values of the original table.
select
ps.project_task_id,
ps.status_id,
count(w.status_id) as total
from (
select distinct
project_task_id,
s.status_id
from work_unit_status
cross join (select distinct status_id from work_unit_status) s
) ps
left join work_unit_status w
on ps.project_task_id = w.project_task_id and ps.status_id = w.status_id
group by
ps.project_task_id,
ps.status_id
If you really need to hardcode the statuses from 1 to 7, use the query below.
select
ps.project_task_id,
ps.status_id,
count(w.status_id) as total
from (
select distinct
project_task_id,
s.status_id
from work_unit_status
cross join (
select 1 as status_id
union select 2
union select 3
union select 4
union select 5
union select 6
union select 7
) s
) ps
left join work_unit_status w
on ps.project_task_id = w.project_task_id and ps.status_id = w.status_id
group by
ps.project_task_id,
ps.status_id
order by
ps.project_task_id,
ps.status_id

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
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