using sum,count on the same column in mysql - mysql

I have a table where i have actions and messages as columns i want to count of particular actions on particular datetime i was able to sum on every action but not count on particular action.
SELECT DATE(datetime),carpark_name,
Sum(CASE action when '2' then 1 else 0 end) as AcceptedIMG,
Sum(CASE action when '3' then 1 else 0 end) as RejectedIMG,
Sum(CASE action when '4' then 1 else 0 end) as ChangeIMG,
sum(CASE action when '23' then 1 else 0 end) as AcceptedViolation,
sum(CASE action when '24' then 1 else 0 end) as RejectedViolation,
sum(CASE action when '25' then 1 else 0 end) as SkippedViolation,
FROM customer_1.audit_trail inner join customer_1.carparks on
customer_1.audit_trail.location_id = customer_1.carparks.id
where DATE(datetime)> '2013-12-01'and DATE(datetime)< '2013-12-03' and location_id = '146'
But this is what I need adding it count(AcceptedIMG, RejectedIMG,ChangeIMG,) or (count(action(2,3,4) as review. I am not able to do this.

To get the SUM of action 2,3 and 4, as column REVIEW and 23,24,25 as CONTRAVENTIO you could do:
SELECT DATE(datetime),
carpark_name,
Sum(CASE action when '2' then 1 else 0 end) as AcceptedIMG,
Sum(CASE action when '3' then 1 else 0 end) as RejectedIMG,
Sum(CASE action when '4' then 1 else 0 end) as ChangeIMG,
SUM(CASE WHEN action IN ('2','3','4') then 1 else 0 end) as REVIEW
sum(CASE action when '23' then 1 else 0 end) as AcceptedViolation,
sum(CASE action when '24' then 1 else 0 end) as RejectedViolation,
sum(CASE action when '25' then 1 else 0 end) as SkippedViolation,
SUM(CASE WHEN action IN ('23','24','25') then 1 else 0 end) as CONTRAVENTION
FROM customer_1.audit_trail
INNER join customer_1.carparks
ON customer_1.audit_trail.location_id = customer_1.carparks.id
WHERE DATE(datetime) > '2013-12-01'
AND DATE(datetime) < '2013-12-03'
AND location_id = '146'
GROUP BY DATE(datetime),carpark_name
I just added the two columns to your query. If you don't need the individual ones, you can remove them.

Related

sql How to hide row if entire row value is 0

My Query :
SELECT
(ref_intake.intakeno) AS intake,
SUM(CASE WHEN(student.intakeid AND student.status = '3' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as active,
SUM(CASE WHEN(student.intakeid AND student.status = '5' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as deffered,
SUM(CASE WHEN(student.intakeid AND student.status = '16' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as inactive,
SUM(CASE WHEN(student.intakeid AND student.status = '9' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as missing,
SUM(CASE WHEN(student.intakeid AND student.status = '6' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as widthdrawn,
SUM(CASE WHEN(student.intakeid AND student.status IN (18,7,36) AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as dis_ter_dereg
FROM student
LEFT JOIN ref_intake ON student.intakeid = ref_intake.intakeid
GROUP BY student.intakeid ORDER BY ref_intake.intakeno ASC
The the image is my result:
I do suspect that your query can be simplified as follows:
SELECT
s.intakeid AS intake,
SUM(CASE WHEN s.status = 3 THEN 1 ELSE 0 END) as active,
SUM(CASE WHEN s.status = 5 THEN 1 ELSE 0 END) as deffered,
SUM(CASE WHEN s.status = 16 THEN 1 ELSE 0 END) as inactive,
SUM(CASE WHEN s.status = 9 THEN 1 ELSE 0 END) as missing,
SUM(CASE WHEN s.status = 6 THEN 1 ELSE 0 END) as widthdrawn,
SUM(CASE WHEN s.status IN (18, 7, 36) THEN 1 ELSE 0 END) as dis_ter_dereg
FROM student s
INNER JOIN ref_intake ri ON s.intakeid = ri.intakeid
WHERE
s.intakeid
AND s.status IN (3, 5, 16, 9, 6, 18, 7, 36)
AND s.courseid IN (3, 17)
GROUP BY s.intakeid
ORDER BY ri.intakeno ASC
That is:
most conditions can be moved to the WHERE clause - this should actually remove rows where no condition matches
it seems like you actually want an INNER JOIN rather than a LEFT JOIN - but you can revert that change if needed
table aliases make the query easier to read and write
ORed conditions can be simplified to use IN

Count each day with SQL query

I want to display a table with SQL query like this:
I have run my query:
select
tb_r_orderdata.finishtime as date ,
count(*)sum all,
sum(when status = 'SUCCESS' and issync = '1' then 1 else 0 end) sumpaid,
sum(when status = 'SUCCESS' and issync in ('3', '4') then 1 else 0 end) sumfail,
sum(when status = 'CLOSED' then 1 else 0 end) sumclose,
sum(when status = 'Null' then 1 else 0 end) sumunflag
from
tb_r_orderdata;
But when I execute it, the result is different than what I expected. The result is like this:
Thank you for any help
You are missing the GROUP BY and the CASE:
select tb_r_orderdata.finishtime as date ,
COUNT(*) as sumall,
SUM(CASE WHEN status='SUCCESS' AND issync='1' then 1 ELSE 0 END) as sumpaid,
SUM(CASE WHEN status='SUCCESS' AND issync in ('3','4') then 1 ELSE 0 END) as sumfail,
SUM(CASE WHEN status='CLOSED' then 1 ELSE 0 END) as sumclose,
SUM(CASE WHEN status is null then 1 ELSE 0 END) as sumunflag
from tb_r_orderdata
group by tb_r_orderdata.finishtime ;
MySQL treats booleans as integers in a numeric context, with "1" for true and "0" for false. You can simplify your query to:
select o.finishtime as date ,
COUNT(*) as sumall,
SUM(status = 'SUCCESS' AND issync = '1') as sumpaid,
SUM(status = 'SUCCESS' AND issync in ('3', '4')) as sumfail,
SUM(status = 'CLOSED') as sumclose,
SUM(status is null) as sumunflag
from tb_r_orderdata o
where tb_r_orderdata.finishtime is not NULL
group by o.finishtime ;
This also removes NULL finish times.
Explanation in comment for Gordon Linoff
i have try your answer but there is record NULL in the top of table, i dont know why, can you explain it
with little bit change from Gordon Linoff answer
select tb_r_orderdata.finishtime as date ,
SUM(CASE WHEN status='SUCCESS' AND issync='1' then 1 ELSE 0 END) as sumpaid,
SUM(CASE WHEN status='SUCCESS' AND issync in ('3','4') then 1 ELSE 0 END) as sumfail,
SUM(CASE WHEN status='CLOSED' then 1 ELSE 0 END) as sumclose,
SUM(CASE WHEN status='NULL' then 1 ELSE 0 END) as sumunflag
from tb_r_orderdata
group by tb_r_orderdata.finishtime

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'

Grouping data by type and displaying

I have a table called 'product', with a 'created_at' date, and a 'type' which is a varchar.
Essentially what I want is an output of the COUNT(*) of each product for every day, for all of the product types. I know that there are only 5 different values for 'type', so that would narrow it down a bit with the query I imagine.
Example:
I have been playing around with this with no success, can't seem to wrap my mind around it. Any ideas?
Try this query
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
GROUP BY created_at
For specific date
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
created_at
WHERE created_at = '2015-12-21'
GROUP BY created_at
With date format
SELECT
SUM(CASE WHEN type = 'type1' THEN 1 ELSE 0 END) AS type1_count,
SUM(CASE WHEN type = 'type2' THEN 1 ELSE 0 END) AS type2_count,
SUM(CASE WHEN type = 'type3' THEN 1 ELSE 0 END) AS type3_count,
SUM(CASE WHEN type = 'type4' THEN 1 ELSE 0 END) AS type4_count,
DATE_FORMAT(created_at,'%Y/%m/%d') AS created_date
GROUP BY created_at
HAVING created_date = '2015/12/21'

i cant able to sort (Ascending/Descending) this part of MySql query?

i want to sort these elements either ascending / descending.
Part of query:
sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END) as optcount1,
sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END) as optcount2,
sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END) as optcount3,
sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END) as optcount4,
Full query:
SELECT 39 AS ques_id,215 as optid1,216 as optid2,217 as optid3,218 as optid4,
'Easy to start the business' as optans1,
'Lower tax rate than a corporation' as optans2,
'Liability is shared' as optans3,
'Owner has total control and say over business' as optans4,
sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END) as optcount1,
sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END) as optcount2,
sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END) as optcount3,
sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END) as optcount4,
'217' as answer, 4 as count FROM `user_training_answers_statistics` as u,
answers as a WHERE a.question_id='39' AND u.answer_id=a.answer_id
Anyone please give me the solution for this..
Wrap it in another query then do the sorting:
select * from
(SELECT 39 AS ques_id,215 as optid1,216 as optid2,217 as optid3,
218 as optid4, 'Easy to start the business' as optans1,
'Lower tax rate than a corporation' as optans2,
'Liability is shared' as optans3,
'Owner has total control and say over business' as optans4,
sum(CASE WHEN a.question_id=39 AND u.answer_id =215 THEN 1 ELSE 0 END)
as optcount1,
sum(CASE WHEN a.question_id=39 AND u.answer_id =216 THEN 1 ELSE 0 END)
as optcount2,
sum(CASE WHEN a.question_id=39 AND u.answer_id =217 THEN 1 ELSE 0 END)
as optcount3,
sum(CASE WHEN a.question_id=39 AND u.answer_id =218 THEN 1 ELSE 0 END)
as optcount4, '217' as answer, 4 as count
FROM user_training_answers_statistics as u,
answers as a
WHERE a.question_id='39' AND u.answer_id=a.answer_id) as tmpTable
ORDER BY optcount1, optcount2, optcount3, optcount4 ASC;