sql - MySql query group by day and by time -
I am trying to create a SQL query to solve my problem.
My table:
+ ---- + --------------------- + ------- + | ID | Date | Price | + ---- + --------------------- + ------- + | 1 | 2014-10-10 05:10:10 | 10 | + ---- + --------------------- + ------- + | 2 | 2014-10-10 09:10:10 | 20 | + ---- + --------------------- + ------- + | 3 | 2014-10-10 15:10:10 | 30 | + ---- + --------------------- + ------- + | 4 | 2014-10-10 23:10:10 | 40 | + ---- + --------------------- + ------- + | 5 | 2014-10-11 08:10:10 | 15 | + ---- + --------------------- + ------- + | 6 | 2014-10-11 09:10:10 | 25 | + ---- + --------------------- + ------- + | 7 | 2014-10-11 10:10:10 | 30 | + ---- + --------------------- + ------- + | 8 | 2014-10-11 23:10:10 | 40 | + ---- + --------------------- + ------- + I want to The time period in the day by group and on this day in three sub groups 'morning' (06:00 - 12:00), 'noon' (12:00 - 18:00) and 'night' (00:00 - 06:00 and 18:00 - 24:00)
Something like this:
+ ------------ + ------- + --------- + ----------- + ------- + | Date | Price | Morning Noon Night + ------------ + ------- + --------- + ----------- + ------ - + | 2014-10-10 | 100 | 20 | 30 | 50 | + ------------ + ------- + --------- + ----------- + ------ - + | 2014-10-11 | 110 | 70 | 0 | 40 | + ------------ + ------- + --------- + ----------- + ------ - +
You can use some sum s More than case expressions:
select day (`date`) AS 'date` (case when hours (` date`) 6 and The value between 12 AD again) in the form of `morning`, (in case of case after 12 and 18 in value ('date'), 'afternoon', case (case when hours (` date`) And or 6; or hour (`date`) & gt; 18 THEN value ELSE 0 END) Day Mera_tebl Group Am` ( `Tithi`)
Comments
Post a Comment