how to get column count - mysql

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

Related

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;

make 2-dimension coordinate using mysql select results

I have question about mysql queries.
I have a table which have data below.
From To Weight
--------------
A B 1
A C 3
B C 2
D E 4
And I want to get sql result like below..
(?) A B C D E
----------------------
A 0 1 3 0 0
B 0 0 2 0 0
C 0 0 0 0 0
D 0 0 0 0 4
E 0 0 0 0 0
And what data is in original table is not determined.
How can I acheive this?
If you know the original columns, you can do:
select c.col1,
sum(case when to = 'A' then weight else 0 end) as a,
sum(case when to = 'B' then weight else 0 end) as b,
sum(case when to = 'C' then weight else 0 end) as c,
sum(case when to = 'D' then weight else 0 end) as d,
sum(case when to = 'E' then weight else 0 end) as d
from (select 'A' as col1 union all select 'B' union all select 'C' union all select 'D' union all select 'E'
) c left join
t
on t.from = c.col1
group by c.col1;
If you don't know the original columns, you could combine the values into a single string:
select col1.col,
group_concat(col2.col, ':', t.weight order by col2.col)
from ((select `from` as col from t
) union -- on purpose to remove duplicates
(select `to` from t
)
) col1 cross join
((select `from` as col from t
) union -- on purpose to remove duplicates
(select `to` from t
)
) col2 left join
t
on col1.col = t.`from` and col2.col = t.`from`
group by col1.col;
If you actually want separate columns and don't know the values, then you would need dynamic SQL.

combine multiple queries with count and group

How do i combine the two mysql queries below to a single query?
SELECT R12NC, COUNT(*) AS `count1` FROM alink where Option_a = 1 GROUP BY R12NC
SELECT R12NC, COUNT(*) AS `count2` FROM alink where Option_x = 1 GROUP BY R12NC
Two ways. If you want just two columns, use a UNION
SELECT R12NC, COUNT(*) AS `count1` FROM alink where Option_a = 1 GROUP BY R12NC
UNION
SELECT R12NC, COUNT(*) AS `count2` FROM alink where Option_x = 1 GROUP BY R12NC
If you want 3 columns then something like:
SELECT R12NC, SUM(CASE WHEN Option_a = 1 THEN 1 ELSE 0 END) as `count1`,
SUM(CASE WHEN Option_x = 1 THEN 1 ELSE 0 END) as `count2`
FROM alink
GROUP BY R12NC
By using the case statement, nested in an aggregation function:
SELECT R12NC,
sum(case when Option_a = 1 then 1 else 0 end) as `count1`,
sum(case when Option_x = 1 then 1 else 0 end) as `count2`
FROM alink
GROUP BY R12NC

MySQL order rank as a column

I have a MySQL query that selects data from multiple tables and then orders the results based on some arbitrary criteria as below:
SELECT [columns] FROM (
SELECT *, COUNT(*) as `matches`
FROM [table1]
JOIN [table2] USING (id)
JOIN [table3] USING (id)
WHERE [criteria]
GROUP BY `id`
ORDER BY `matches` DESC
) AS `grouped`
ORDER BY (
(CASE WHEN [1st rank criteria] THEN 3 ELSE 0 END) +
(CASE WHEN [2nd rank criteria] THEN 2 ELSE 0 END) +
(CASE WHEN [3rd tank criteria] THEN 1 ELSE 0 END)
) DESC
LIMIT 100
This works fine, but my question is: can I have the ranking score displayed as a column? I've looked at trying to use variables, but I'm quite new to SQL so all this is a bit beyond me.
Sorry if this is an obvious question, but many thanks in advance for your time and assistance.
Try this:
SELECT [columns],
(
(CASE WHEN [1st rank criteria] THEN 3 ELSE 0 END) +
(CASE WHEN [2nd rank criteria] THEN 2 ELSE 0 END) +
(CASE WHEN [3rd tank criteria] THEN 1 ELSE 0 END)
) AS MyRank
FROM (
SELECT *, COUNT(*) as `matches`
FROM [table1]
JOIN [table2] USING (id)
JOIN [table3] USING (id)
WHERE [criteria]
GROUP BY `id`
ORDER BY `matches` DESC
) AS `grouped`
ORDER BY MyRank DESC
LIMIT 100;
put it in the select
((CASE WHEN [1st rank criteria] THEN 3 ELSE 0 END) +
(CASE WHEN [2nd rank criteria] THEN 2 ELSE 0 END) +
(CASE WHEN [3rd tank criteria] THEN 1 ELSE 0 END)) as ranking

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