Mysql Case - Count Fields thats fall in curtain hours - mysql

I am trying to set up a report that has the following fields :
Pretty much need the report to show the date, the total records that fall with in that date (so I group by Date) and then on a per hour basis for a 12 hour work day (from 8AM - 8PM) I need to count when a records is present within those times. After some brain storming and brain farts I thought why not use a case. This is what I have tried :
SELECT
DATE_FORMAT(signintime, '%b %d, %Y') Date,
COUNT(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) > 08 AND HOUR(signintime) < 09 THEN 1 ELSE 0 END) '8AM-9PM',
SUM(CASE WHEN HOUR(signintime) > 09 AND HOUR(signintime) < 10 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) > 10 AND HOUR(signintime) < 11 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) > 11 AND HOUR(signintime) < 12 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) > 12 AND HOUR(signintime) < 13 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) > 13 AND HOUR(signintime) < 14 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) > 14 AND HOUR(signintime) < 15 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) > 15 AND HOUR(signintime) < 16 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) > 16 AND HOUR(signintime) < 17 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) > 17 AND HOUR(signintime) < 18 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) > 18 AND HOUR(signintime) < 19 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) > 19 AND HOUR(signintime) < 20 THEN 1 ELSE 0 END) '7PM-8PM'
FROM session
WHERE session.status = '3'
GROUP by HOUR(signintime), Date;
If you notice the picture above, the total is 1 for that entire day (Apr 19, 2013) now if you notice the times (8AM - 8PM) they are all zeroed out. I am unsure as to where to turn to / where to debug this. Hopefully with another set of eyes I can get this moving along.
Regards.

You should not need to use both the > and < to get the result, you can just check the hour value:
select DATE_FORMAT(signintime, '%b %d, %Y') Date,
count(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) = 08 THEN 1 ELSE 0 END) '8AM-9AM',
SUM(CASE WHEN HOUR(signintime) = 09 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) = 10 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) = 11 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) = 12 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) = 13 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) = 14 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) = 15 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) = 16 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) = 17 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) = 18 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) = 19 THEN 1 ELSE 0 END) '7PM-8PM'
from session
WHERE session.status = '3'
group by DATE_FORMAT(signintime, '%b %d, %Y');
See SQL Fiddle with Demo

Related

How to change result SQL syntax for age group from colomn to new row

I Tried to grouping age from and found this answer(SQL Group by Age Range)
But the result is as a colomn not a row Like this
I Just want to convert the colomn into a row Like This
The query is:
SELECT SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') < 18 THEN 1 ELSE 0 END) AS '< 18',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 18 AND 25 THEN 1 ELSE 0 END) AS '18-25 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 26 AND 35 THEN 1 ELSE 0 END) AS '26-35 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 36 AND 45 THEN 1 ELSE 0 END) AS '36-45 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 46 AND 55 THEN 1 ELSE 0 END) AS '46-55 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 56 AND 65 THEN 1 ELSE 0 END) AS '56-65 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 66 AND 200 THEN 1 ELSE 0 END) AS '>66 Tahun'
FROM user_detail
My table for tanggal_lahir:

Get Data for Grouping all Months in a year using MySql and DB2

below is my query
SELECT DISTINCT e2.status as status, e2.year as year, e2.month as month,e2.Jan,e2.Feb,e2.Mar,e2.Apr,e2.May,e2.Jun,e2.Jul,e2.Aug,e2.Sep,e2.Oct,e2.Nov, e2.Dece, e2.countTotal FROM tranx_history e1 INNER JOIN (
SELECT YEAR(e.create_dt) AS year, MONTH(e.create_dt) AS month,
SUM(CASE WHEN MONTH(e.create_dt) = 01 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(e.create_dt) = 02 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(e.create_dt) = 03 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE WHEN MONTH(e.create_dt) = 04 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE WHEN MONTH(e.create_dt) = 05 THEN 1 ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(e.create_dt) = 06 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE WHEN MONTH(e.create_dt) = 07 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE WHEN MONTH(e.create_dt) = 08 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE WHEN MONTH(e.create_dt) = 09 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE WHEN MONTH(e.create_dt) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN MONTH(e.create_dt) = 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE WHEN MONTH(e.create_dt) = 12 THEN 1 ELSE 0 END) AS Dece,
SUM(CASE WHEN MONTH(e.create_dt) >= 01 AND MONTH(e.create_dt) <= 12 THEN 1 ELSE 0 END) as countTotal,
e.trnx_status as status
from tranx_history e
GROUP BY e.trnx_status, MONTH(e.create_dt),YEAR(e.create_dt) ) e2 ON YEAR(e1.create_dt) = e2.year AND MONTH(e1.create_dt) = e2.month ORDER BY e2.year asc, e2.month asc
For the Above query, The Output we are getting is first it is grouping by Jan then in the next row it is grouping by Feb even though status and year are same
i want the output Jan and Feb with same status and year in a single row not in different rows

Query to find rows with 2 consecutive months (colums) that are negative

I have a SQL Server 2008 table with columns representing each 12 months. I want to find rows where there are 2 consecutive month with a negative balance.
Edit: I needed to sum all the positive columns so I used this. If anybody knows an easier way let me know.
SELECT SUM(case when Oct > 0 then Oct else 0 end) +
SUM(case when Nov > 0 then Nov else 0 end) +
SUM(case when Dec > 0 then Dec else 0 end) +
SUM(case when Jan > 0 then Jan else 0 end) +
SUM(case when Feb > 0 then Feb else 0 end) +
SUM(case when Mar > 0 then Mar else 0 end) +
SUM(case when Apr > 0 then Apr else 0 end) +
SUM(case when May > 0 then May else 0 end) +
SUM(case when Jun > 0 then Jun else 0 end) +
SUM(case when Jul > 0 then Jul else 0 end) +
SUM(case when Aug > 0 then Aug else 0 end) +
SUM(case when Sep > 0 then Sep else 0 end) as 'Sum of positive months'
You can do it straightforward:
select * from tablename
where (Oct < 0 and Nov < 0) or
(Nov < 0 and Dec < 0) or
(Dec < 0 and Jan < 0) or
(Jan < 0 and Feb < 0) or
...

SQL Group by "Folder Name" by month and then total

My new query is giving me few errors:
Error: ORA-0093: SQL command not properly ended.
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month (a.sent_date)=1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date)=2 then a.total_sent else 0 end) as February,
sum(case when month(a.sent_date)=3 then a.total_sent else 0 end) as March,
sum(case when month(a.sent_date)=4 then a.total_sent else 0 end) as April,
sum(case when month(a.sent_date)=5 then a.total_sent else 0 end) as May,
sum(case when month(a.sent_date)=6 then a.total_sent else 0 end) as June,
sum(case when month(a.sent_date)=7 then a.total_sent else 0 end) as July,
sum(case when month(a.sent_date)=8 then a.total_sent else 0 end) as August,
sum(case when month(a.sent_date)=9 then a.total_sent else 0 end) as September,
sum(case when month(a.sent_date)=10 then a.total_sent else 0 end) as October,
sum(case when month(a.sent_date)=11 then a.total_sent else 0 end) as November,
sum(case when month(a.sent_date)=12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id=123 AND
a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;
=========
This is my first time posting here and also a beginner at queries.
I am running a query which returns various folder names for all days. I want to group by the folder name and do a sum of the totals for each folder name by months and then a total of each column at the bottom. This is the query I am running:
select a.group_name, a.sent_date, a.total_sent
from
c_group a
where
a.partner_id=123
and a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
Displays as follows:
GROUP_NAME SENT_DATE TOTAL_SENT
Group A 1-Jan-12 37
Group B 3-Jan-12 25
Group C 1-May-12 10
Group D 1-May-12 8
Group D 1-Jan-12 11
Group A 1-Dec-12 9
I need the results to display as:
January February March April May June July August September October November December
Group A
Group B
Group C
Group D
...
....
...
....
Total Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above
You want to combine conditional aggregation with rollup:
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month(a.sent_date) = 1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date) = 2 then a.total_sent else 0 end) as February,
. . .
sum(case when month(a.sent_date) = 12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id = 123 AND
a.sent_date >= '01-JAN-2012' AND
a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;

Calculate AVG of each Month excluding Months w/zeroes

I have the following working query that sums the length column/values for each month.
SELECT SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 1 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 2 THEN (tblcm.Lgth) ELSE 0 END) AS 'Feb',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 3 THEN (tblcm.Lgth) ELSE 0 END) AS 'Mar',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 4 THEN (tblcm.Lgth) ELSE 0 END) AS 'Apr',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 5 THEN (tblcm.Lgth) ELSE 0 END) AS 'May',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 6 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jun',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 7 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jul',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 8 THEN (tblcm.Lgth) ELSE 0 END) AS 'Aug',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 9 THEN (tblcm.Lgth) ELSE 0 END) AS 'Sep',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 10 THEN (tblcm.Lgth) ELSE 0 END) AS 'Oct',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 11 THEN (tblcm.Lgth) ELSE 0 END) AS 'Nov',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 12 THEN (tblcm.Lgth) ELSE 0 END) AS 'Dec',
SUM(tblcm.Lgth) AS Total
giving me the following:
Jan 13050
Feb 5200
Mar 48450
Apr 34041
May 38000
Jun 0
Jul 0
Aug 0
Sep 0
Oct 0
Nov 0
Dec 0
How can I get the AVG of only the months greater than zero?
I tried: avg(nullif(tblcm.Lgth, 0)) as m_Avg but get 1825
I also tried: avg(case when tblcm.Lgth = 0 then null else tblcm.Lgth end) as m_Avg but also get 1825
I need to get 27748 (which is the SUM of Jan thru May totals, divided by 5 months)
I'm pretty sure that you'll need a subquery to do that. The following assumes that your original query is correct as posted and functions as you said. This is untested, and your probably could have included some more details in your question, so let me now if this doesn't suit your needs.
SELECT AVG(sumLgth) AS avgLgth, SUM(sumLgth) AS totalLgth,
SUM(CASE WHEN theMonth = 1 THEN (tbl.sumLgth) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN theMonth = 2 THEN (tbl.sumLgth) ELSE 0 END) AS 'Feb',
...
FROM (
SELECT MONTH(omActCompDate) AS theMonth, SUM(lgth) AS sumLgth
FROM tblcm
GROUP BY MONTH(omActCompDate)
HAVING sumLgth > 0
) tbl