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

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

Related

SQL - Calculate percentage of multiple counts

I wrote a query to count and group all items in db by it's status.
Now, I need to calculate percentage of every given result.
SELECT
COUNT(CASE WHEN status='Pending' THEN 1 ELSE NULL END) as 'Pennding Requests',
COUNT(CASE WHEN status='Accepted' THEN 1 ELSE NULL END) as 'Accepted Requests',
COUNT(CASE WHEN status='Denied' THEN 1 ELSE NULL END) as 'Denied Requests'
FROM meeting_request;
which gave me:
enter image description here
I tried something like:
100.0 * (id / Pennding Requests) AS PERCENTAGE
Can some one suggest how to calculate percentage all of three status.
You can use AVG():
SELECT AVG( status = 'Pending' ) as pending_requests,
AVG( status = 'Accepted' ) as Accepted_Requests,
AVG( status = 'Denied' ) as Denied_Requests
FROM meeting_request;
Because MySQL interprets booleans as numbers with 1 for true and 0 for false, you can use the handy shortcut. The more formal SQL code is:
SELECT AVG(CASE WHEN status = 'Pending' THEN 1.0 ELSE 0 END)
Have you tried doing it like this?
SELECT
COUNT(CASE WHEN status='Pending' THEN 1 ELSE NULL END) / SUM(1) * 100.0 as 'Pennding Requests Percent',
COUNT(CASE WHEN status='Accepted' THEN 1 ELSE NULL END) / SUM(1) * 100.0 as 'Accepted Requests Percent',
COUNT(CASE WHEN status='Denied' THEN 1 ELSE NULL END) / SUM(1) * 100.0 as 'Denied Requests Percent'
FROM meeting_request;

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'

SQL using CASE in count and group by

I'm using CASE to categorize data in the table and count them but the results aren't accurate
live demo [here]
select DATE(date) as day, count(*),
count(distinct case when name = 'fruit' then 1 else 0 end) as fruits,
count(distinct case when name = 'vege' then 1 else 0 end) as vege,
count(distinct case when name = 'sweets' then 1 else 0 end) as sweets
from food
group by day
with rollup
I'm not sure if the issue is with CASE or in the string matching = because there's no 'sweets' still it counts 1?
any pointers I'd be grateful
Your problem is that COUNT counts every result that is not NULL. In your case you are using:
COUNT(distinct case when name = 'sweets' then 1 else 0 end)
So, when the name is not sweets, it counts the 0. Furthermore, since you are using DISTINCT, it counts just one or two values. You should either use SUM or remove the DISTINCT and the ELSE 0:
SELECT DATE(date) as day,
COUNT(*),
SUM(CASE WHEN name = 'fruit' THEN 1 ELSE 0 END) as fruits,
SUM(CASE WHEN name = 'vege' THEN 1 ELSE 0 END) as vege,
SUM(CASE WHEN name = 'sweets' THEN 1 ELSE 0 END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Or:
SELECT DATE(date) as day,
COUNT(*),
COUNT(CASE WHEN name = 'fruit' THEN 1 ELSE NULL END) as fruits,
COUNT(CASE WHEN name = 'vege' THEN 1 ELSE NULL END) as vege,
COUNT(CASE WHEN name = 'sweets' THEN 1 ELSE NULL END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Here is a modified sqlfiddle.
You can't group by an alias. You have to group by the expression.
group by date(date)
You can group on an Alias:
SELECT
FROM_UNIXTIME(UnixTimeField, '%Y') AS 'Year'
,FROM_UNIXTIME(UnixTimeField, '%m') AS 'Month'
FROM table p
GROUP BY Year, Month

Sum columns in Count Case 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

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