Sum columns in Count Case MYSQL - mysql

I am trying to total up the columns in a Count Case query. I have the following query
SELECT distinct type,
Year(`date_ready`) as Year,
Count(case when month(`date_ready`)=1 then `type` end) As Jan_Attr,
Count(case when month(`date_ready`)=2 then `type ` end) As Feb_Attr,
Count(case when month(`date_ready`)=3 then `type ` end) As Mar_Attr,
Count(case when month(`date_ready`)=4 then `type ` end) As Apr_Attr,
Count(case when month(`date_ready`)=5 then `type ` end) As May_Attr,
Count(case when month(`date_ready`)=6 then `type ` end) As Jun_Attr,
Count(case when month(`date_ready`)=7 then `type ` end) As Jul_Attr,
Count(case when month(`date_ready`)=8 then `type ` end) As Aug_Attr,
Count(case when month(`date_ready`)=9 then `type ` end) As Sep_Attr,
Count(case when month(`date_ready`)=10 then `type ` end) As Oct_Attr,
Count(case when month(`date_ready`)=11 then `type ` end) As Nov_Attr,
Count(case when month(`date_ready`)=12 then `type ` end) As Dec_Attr
FROM newsuit
GROUP BY Year(`date_ready`), type
What I would like is to get a row called Total, then the total for Jan, total for Feb etc. I presume I need a Sum somewhere, but whatever I try seems to give me the total in the wrong column. I'm guessing that maybe I need to wrap the whole query in another, but I can't seem to get the syntax right.
If I put Sum(type) as Total in the select, I just get one cell with the total in. All the other things I have tried give me errors.
Cheers....
Additional discovery. I've found that the following addition before the From clause adds totals to each row. This is useful, however I still want to find the totals for the columns :)
sum(case when month(`date_ready`) <13 then 1 else 0 end) as Total
Further additional discovery....
This gives me the column totals, just need to combine the two queries.
Select Sum(PQ.Jan_Attr) As JanTot, Sum(PQ.Feb_Attr) As FebTot,Sum(PQ.Mar_Attr) As MarTot,Sum(PQ.Apr_Attr) As AprTot,Sum(PQ.May_Attr) As MayTot,Sum(PQ.Jun_Attr) As JunTot,
Sum(PQ.Jul_Attr) As JulTot,Sum(PQ.Aug_Attr) As AugTot,Sum(PQ.Sep_Attr) As SepTot,Sum(PQ.Oct_Attr) As OctTot,Sum(PQ.Nov_Attr) As NovTot ,Sum(PQ.Dec_Attr) As DecTot
From
(SELECT distinct type,
Year(`date_ready`) as Year,
Count(case when month(`date_ready`)=1 then `type` end) As Jan_Attr,
Count(case when month(`date_ready`)=2 then `type` end) As Feb_Attr,
Count(case when month(`date_ready`)=3 then `type` end) As Mar_Attr,
Count(case when month(`date_ready`)=4 then `type` end) As Apr_Attr,
Count(case when month(`date_ready`)=5 then `type` end) As May_Attr,
Count(case when month(`date_ready`)=6 then `type` end) As Jun_Attr,
Count(case when month(`date_ready`)=7 then `type` end) As Jul_Attr,
Count(case when month(`date_ready`)=8 then `type` end) As Aug_Attr,
Count(case when month(`date_ready`)=9 then `type` end) As Sep_Attr,
Count(case when month(`date_ready`)=10 then `type` end) As Oct_Attr,
Count(case when month(`date_ready`)=11 then `type` end) As Nov_Attr,
Count(case when month(`date_ready`)=12 then `type` end) As Dec_Attr,
sum(case when month(`date_ready`) <13 then 1 else 0 end) as Total
FROM newsuit
WHERE Year(`date_ready`) = '2013'
GROUP BY Year(`date_ready`), type) as PQ
UPDATE 13/02/2014 - I've solved this one now, sort of. I couldn't get what I wanted in a single query, so in the event, I've run the two queries above and just used PHP to display it all as one table. Seemed to be the simplest way in the end.
If anyone would like to know how I did this, I'll happily post / send the code I used to create the tables and format it for displaying data from multiple queries.

I was just asking a similar question, why not try this:
sum(case when year(`date_ready`) = '2013' then 1 else 0 end) as Total

How about
SELECT
suit_type,
Year(`date_ready`) as Year,
SUM(IF(month(`date_ready`) = 1,`type`,0)) AS Jan_Attr,
...
FROM newsuit
GROUP BY Year(`date_ready`), suit_type

Related

Monthly Counter Query in MySQL

I wrote a query. The query and its output can be found below.
Well, I need a query that takes this query as subquery and it will produce output like below. I prepared some queries but they didn't work, generaly they brought null for each month value. There might be another better apporach for this kind of problems. I have just tried like that.
Ideally, you will have to use something like PIVOT() in SQL. Unfortunately, it is not available in MySQL. So this will work:
SELECT customer,
MAX(CASE WHEN month='January' THEN total_count ELSE 0 END) AS Jan,
MAX(CASE WHEN month='February' THEN total_count ELSE 0 END) AS Feb,
MAX(CASE WHEN month='March' THEN total_count ELSE 0 END) AS Mar,
MAX(CASE WHEN month='April' THEN total_count ELSE 0 END) AS Apr,
MAX(CASE WHEN month='May' THEN total_count ELSE 0 END) AS May,
MAX(CASE WHEN month='June' THEN total_count ELSE 0 END) AS Jun,
MAX(CASE WHEN month='July' THEN total_count ELSE 0 END) AS Jul,
MAX(CASE WHEN month='August' THEN total_count ELSE 0 END) AS Aug,
MAX(CASE WHEN month='September' THEN total_count ELSE 0 END) AS Sep,
MAX(CASE WHEN month='October' THEN total_count ELSE 0 END) AS Oct,
MAX(CASE WHEN month='November' THEN total_count ELSE 0 END) AS Nov,
MAX(CASE WHEN month='December' THEN total_count ELSE 0 END) AS Decem
FROM t
GROUP BY customer
Note:
t must be replaced with your entire query as sub-query
Taking MAX, MIN or SUM will give us the same result as it is already grouped at month and customer level
If you wanna use Dec for last column name, use it as "Dec" otherwise MySQL will throw an error (due to keyword is used)
Look at the Demo in db<>fiddle

Can I get average of a column in mySQL DB based upon value of other column in one query?

I have a table of phone call activity for a client. In the table, I have one column for the length of the call (in seconds), and another column for "first time call" (true / false). I was hoping to find a way to get the average call length of first time calls separated from the average time of non first time calls? Is this doable in a singe mySQL query?
SELECT location,
count(*) AS total,
sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall,
sum(case when answered = 'Yes' then 1 else 0 end) AS answered,
sum(case when tags like '%Lead%' then 1 else 0 end) as lead,
sum(case when tags like '%arbage%' then 1 else 0 end) as garbage,
avg(case when duration........firstTime = True???)
FROM staging
GROUP BY location
SELECT location,
count(*) AS total,
sum(case when firstCall = 'true' then 1 else 0 end) AS firstCall,
sum(case when answered = 'Yes' then 1 else 0 end) AS answered,
sum(case when tags like '%Lead%' then 1 else 0 end) as lead,
sum(case when tags like '%arbage%' then 1 else 0 end) as garbage,
sum(case when firstCall='true' then duration else 0 end)/sum(case when firstCall = 'true' then 1 else 0 end) as first_call_true_average,
sum(case when firstCall='false' then duration else 0 end)/sum(case when firstCall = 'false' then 1 else 0 end) as first_call_false_average
FROM staging
GROUP BY location
I would phrase this as:
select
location,
count(*) as total,
sum(firstcall = 'true' ) as cnt_firstcall,
sum(answered = 'Yes' ) as cnt_answered,
sum(tags like '%Lead%' ) as cnt_lead,
sum(tags like '%arbage%') as cnt_garbage,
avg(case when firstcall = 'true' then duration end) as avg_first_call,_duration
avg(case when firstcall = 'false' then duration end) as avg_non_first_call_duration
from staging
group by location
Rationale:
MySQL interpret true/false conditions as 1/0 values in numeric context, which greatly shortens the conditional sum()s
avg() ignores null values, so a simple case expression is sufficient to compute the conditional averages

How to get sum of value for specific field

-------------------------------------------------
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
(SELECT
(SUM( tshares ) AS total WHERE trtm = 'TM'),
(SUM( tshares ) AS total1 WHERE trtm = 'TR'))
FROM transfer_file
where t_date between '10/1/1992' and '10/2/1992'
-----------------------------------------------
How to get the sum of values for specific fields,i am getting the count of value but not getting the sum of value,what to do? help me if any body knows
You should use conditional aggregation inside the SUM too:
SELECT trtm,
COUNT(CASE WHEN trtm= 'TM' THEN 1 ELSE NULL END) AS Transmission,
COUNT(CASE WHEN trtm= 'TR' THEN 1 ELSE NULL END) AS Transfer,
COUNT(CASE WHEN trtm= 'DL' THEN 1 ELSE NULL END) AS Deletion
SUM(CASE WHEN trtm = 'TM' THEN tshares END) AS Total_Transmission,
SUM(CASE WHEN trtm = 'TR' THEN tshares END) AS Total_Transfer
FROM transfer_file
WHERE t_date between '10/1/1992' and '10/2/1992'

Compare columns from SUM(CASE...END) in the same query

I'm trying to add a column named Ready to my query that checks if the sum of Complete is = to the sum of Total and places 'Y' if True and 'N' if FALSE.
Here is my query that works producing 4 columns
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS 'Complete',
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS 'Not Started',
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS 'Total'
FROM `pc`
Group By pc.Sub WITH ROLLUP
I just cant figure out how to create the extra column if it is possible at all.
Regards
Try outer SELECT
SELECT `Complete`,
`Not Started`,
`Total`,
CASE WHEN `Complete` = `Total` THEN 'Y' ELSE 'N' END `Ready`
FROM (
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS `Complete`,
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS `Not Started`,
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS `Total`
FROM `pc`
GROUP BY pc.Sub WITH ROLLUP) t
Try this:
SELECT pc.Sub,
SUM(IF(`SheetStatus`='Complete',1,0) AS 'Complete',
SUM(IF(`SheetStatus`='Not started',1,0) AS 'Not Started',
SUM(IF(`CheckSheet` LIKE '%',1,0) AS 'Total'
IF(SUM(IF(`CheckSheet` LIKE '%',1,0) = SUM(IF(`SheetStatus`='Complete',1,0),"Y","N") AS Ready
FROM `pc`
Group By pc.Sub WITH ROLLUP

Invalid use of group function (For using count in select)

I got an error output on my sql query with remarks " Invalid use of group function "
This is my query
SELECT * FROM
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=6 THEN 1 ELSE NULL END) as `week 6`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=2 THEN 1 ELSE NULL END) as `week 2`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=3 THEN 1 ELSE NULL END) as `week 3`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=4 THEN 1 ELSE NULL END) as `week 4`
FROM tbl_nif,tbl_custmon,tbl_incoming
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment
AND plannedGI!='0000-00-00'
GROUP BY tbl_custmon.so
ORDER BY tbl_custmon.so) AS grid
ORDER BY project ASC
If I not include the "count" in query, it will be show the output.
So, kindly please tell me an idea for this issue.
Big Thanks!
CONCLUSION
Put "Max" function on query makes a natural "group by".
So I put "Max" function inline with the other SELECT field.
So the query will be like this
SELECT * FROM
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc, MAX(plannedGI),
COUNT(CASE WHEN WEEK(plannedGI)+1=6 THEN 1 ELSE NULL END) as `week 6`,
COUNT(CASE WHEN WEEK(plannedGI)+1=2 THEN 1 ELSE NULL END) as `week 2`,
COUNT(CASE WHEN WEEK(plannedGI)+1=3 THEN 1 ELSE NULL END) as `week 3`,
COUNT(CASE WHEN WEEK(plannedGI)+1=4 THEN 1 ELSE NULL END) as `week 4`
FROM tbl_nif,tbl_custmon,tbl_incoming
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment
AND plannedGI!='0000-00-00'
GROUP BY tbl_custmon.so
ORDER BY tbl_custmon.so) AS grid
ORDER BY project ASC
And the output will show data that I need.
Many thanks to you :)