not matching when inside mysql case statement is not showing in result - mysql

I have an mysql statement as below
SELECT CASE
WHEN HOUR(created_at) BETWEEN 0 AND 11 THEN 'Morning'
WHEN HOUR(created_at) BETWEEN 12 AND 15 THEN 'Afternoon'
WHEN HOUR(created_at) BETWEEN 16 AND 18 THEN 'Evening'
WHEN HOUR(created_at) BETWEEN 19 AND 24 THEN 'Night'
END AS session,
SUM(total) AS `total` FROM `orders` WHERE (purchase_date between '2014-05-01' and '2014-05-30')
GROUP BY CASE
WHEN HOUR(created_at) BETWEEN 0 AND 11 THEN 1
WHEN HOUR(created_at) BETWEEN 12 AND 16 THEN 2
WHEN HOUR(created_at) BETWEEN 17 AND 18 THEN 3
WHEN HOUR(created_at) BETWEEN 19 AND 24 THEN 4
END;
I am getting an output like this
+------------+------------+
| session | total |
+------------+------------+
| Morning | 47083.21 |
| Afternoon | 1124804.51 |
| Evening | 165643.34 |
| Night | 1690492.01 |
+------------+------------+
But when there are no entries for morning then the output is missing the Morning row in results but I want an row with morning but total as 0.
please help me how to achieve the same
Expected output
+------------+------------+
| session | total |
+------------+------------+
| Morning | 0 |
| Afternoon | 14804.51 |
| Evening | 16643.34 |
| Night | 19492.01 |
+------------+------------+
actual output is without morning row
Actual output
+------------+------------+
| session | total |
+------------+------------+
| Afternoon | 1124804.51 |
| Evening | 165643.34 |
| Night | 1690492.01 |
+------------+------------+
I would greatly appreciate if Any kind of help or hint is given to solve this problem

Add create table cal (hours int not null). Fill it with 0 to 23 (there is no 24 hour?). Then do
SELECT CASE
WHEN cal.hours BETWEEN 0 AND 11 THEN 'Morning'
WHEN cal.hours BETWEEN 12 AND 15 THEN 'Afternoon'
WHEN cal.hours BETWEEN 16 AND 18 THEN 'Evening'
WHEN cal.hours BETWEEN 19 AND 24 THEN 'Night'
END AS session
,sum(coalesce(total, 0)) from
(select created_at, total from orders
where purchase_date between '2014-05-01' and '2014-05-30') T1
right outer join cal on (cal.hours = hour(T1.created_at))
group by CASE
WHEN cal.hours BETWEEN 0 AND 11 THEN 'Morning'
WHEN cal.hours BETWEEN 12 AND 15 THEN 'Afternoon'
WHEN cal.hours BETWEEN 16 AND 18 THEN 'Evening'
WHEN cal.hours BETWEEN 19 AND 24 THEN 'Night' END;

Related

How to write an SQL query to calculate the average for three consecutive values?

I have a table like this
userID time NoOfVisits
1 2014 50
2 2015 60
3 2016 70
4 2017 80
5 2018 90
6 2019 100
I need to write a sql query which will print time and average of past 3 years NoOfVisits for a particular site.
output should be as
userID time NoOfVisits
1 2014 50.0000
2 2015 55.0000
3 2016 60.0000
4 2017 70.0000
5 2018 80.0000
6 2019 90.0000
Explanation :
For user Id 6 (80+90+100)/3=90.0000
Please help me to solve this problem.
You can use a cumulative average, available in MySQL 8+:
select t.*,
avg(visits) over (order by time rows between 2 preceding and current row) as avg_visits_3
from t;
Assuming there are no gaps between the years (like your sample data), you can self join the table and group by userid, time to get the average:
select
t.userid, t.time, avg(tt.noofvisits) NoOfVisits
from tablename t inner join tablename tt
on tt.time between t.time - 2 and t.time
group by t.userid, t.time
See the demo.
Results:
| userid | time | NoOfVisits |
| ------ | ---- | ---------- |
| 1 | 2014 | 50 |
| 2 | 2015 | 55 |
| 3 | 2016 | 60 |
| 4 | 2017 | 70 |
| 5 | 2018 | 80 |
| 6 | 2019 | 90 |

Group by date every 10 days

I have this scheme:
+----+--+--------+--------------------+
| ID | Amount | paydate |
+----+-----------+--------------------+
| 1 | 200 |2016-11-05 |
+----+-----------+--------------------+
| 2 | 3000 |2016-11-10 |
+----+-----------+--------------------+
| 3 | 2500 |2016-11-11 |
+----+-----------+--------------------+
| ID | 100 |2016-11-21 |
+----+-----------+--------------------+
| 1 | 200 |2016-11-22 |
+----+-----------+--------------------+
| 2 | 3000 |2016-11-23 |
+----+-----------+--------------------+
| 3 | 2500 |2016-11-29 |
+----+-----------+--------------------+
How can I get the total Amount grouped by every 10 days like from the first of every month to the 10th then from 11th to 20th and from 21st to the end of the month?
to be shown like this :
+-----------+------------------------+
| Amount | paydate |
+-----------+------------------------+
| 3200 |2016-11-1 to 2016-11-10 |
+-----------+------------------------+
| 2500 |2016-11-11 to 2016-11-20|
+-----------+------------------------+
| 5800 |2016-11-21 to 2016-11-31|
+-----------+------------------------+
I tried
SELECT
SUM(Amount) AS Amount,
year(Facture.paydate) AS Annee,
month(Facture.paydate) AS Mois
FROM Facture
GROUP BY year(Facture.paydate), month(serFacture.paydate)
but this does not give me the result I need.
select sum(Amount) as sum_amount
,case
when day(paydate) <= 10 then concat(DATE_FORMAT(paydate,'%Y-%m-01'),' to ',DATE_FORMAT(paydate,'%Y-%m-10'))
when day(paydate) <= 20 then concat(DATE_FORMAT(paydate,'%Y-%m-11'),' to ',DATE_FORMAT(paydate,'%Y-%m-20'))
else concat(DATE_FORMAT(paydate,'%Y-%m-21'),' to ',DATE_FORMAT(paydate,'%Y-%m-31'))
end as paydate_period
from t
group by paydate_period
;
sum_amount paydate_period
3200 2016-11-01 to 2016-11-10
2500 2016-11-11 to 2016-11-20
5800 2016-11-21 to 2016-11-31
Here is an example query:
select
case
when day(date_field) between 1 and 10 then "01 to 10"
when day(date_field) between 11 and 20 then "11 to 20"
when day(date_field) between 21 and 31 then "21 to 31"
end as the_range,
date_format(date_field, "%m%Y") as the_month,
count(*)
from
the_table
group by
the_range, the_month
order by
the_month, the_range;
You can adapt the query so you display your result the way you need.

Group and sum worked hours by days

I'm writing a software to manage worked hours. For each day could be different activities.
I wish to extract the worked hours for each worker in the last 7 days.
Sample rows
+----+------------+-------+----------+----------+
| id | date | order | operator | duration |
+----+------------+-------+----------+----------+
| 37 | 2016-06-12 | 27 | 1 | 180 |
| 38 | 2016-06-12 | 28 | 3 | 390 |
| 39 | 2016-06-12 | 27 | 1 | 480 |
| 40 | 2016-06-04 | 21 | 2 | 120 |
| 41 | 2016-05-07 | 27 | 1 | 90 |
| 42 | 2016-06-07 | 27 | 1 | 150 |
+----+------------+-------+----------+----------+
The Query
SELECT SUM(`duration`) as `hours_per_day`
FROM `sheets`
WHERE `operator` = 1 AND `date` = DATE_ADD(CURDATE(), INTERVAL -7 DAY)
ORDER BY `date` DESC
Expected result:
+------------------------+--------+--------+--------+--------+--------+--------+--------+
| Operator: Avareage Joe |
+------------------------+--------+--------+--------+--------+--------+--------+--------+
| Day: | 01 jul | 02 jul | 03 jul | O4 jul | 05 jul | 06 jul | 07 jul |
| Hours: | 8 | 7 | 9 | 8 | 9 | 8 | 6 |
+------------------------+--------+--------+--------+--------+--------+--------+--------+
SQL Fiddle Demo
SELECT
CURDATE(),
SUM(CASE WHEN CURDATE() = `date` THEN `duration` ELSE 0 END) as today ,
SUM(CASE WHEN CURDATE() - INTERVAL 1 DAY = `date` THEN `duration` ELSE 0 END) as yesterday,
SUM(CASE WHEN CURDATE() - INTERVAL 2 DAY = `date` THEN `duration` ELSE 0 END) as `today - 2`,
SUM(CASE WHEN CURDATE() - INTERVAL 3 DAY = `date` THEN `duration` ELSE 0 END) as `today - 3`,
SUM(CASE WHEN CURDATE() - INTERVAL 4 DAY = `date` THEN `duration` ELSE 0 END) as `today - 4`,
SUM(CASE WHEN CURDATE() - INTERVAL 5 DAY = `date` THEN `duration` ELSE 0 END) as `today - 5`,
SUM(CASE WHEN CURDATE() - INTERVAL 6 DAY = `date` THEN `duration` ELSE 0 END) as `today - 6`
FROM `work`
where operator = 1
OUTPUT
Let's try it like this:
SELECT operator, `date`, (SUM(duration) / 60) as hours_per_day
FROM sheets
WHERE `date` > NOW() - INTERVAL 7 DAY
GROUP BY operator, date
ORDER BY operator, date
Just look at the WHERE clause first. We exclude everything that is more than 7 days old.
Then we use GROUP BY to take all the remaining rows and group them by operator and date, so you basically get small sub-results that you can work with.
I retained your SUM(duration) operation, which now calculates sums for every one of these little sub-results. I just added the division, because obviously you're storing minutes, not hours.
Finally we use ORDER BY to make sure the result doesn't look like a big mess.
Your result should look something like this:
operator | date | hours_per_day
---------------------------------------
1 | 2016-07-01 | 4
1 | 2016-07-02 | 6
1 | 2016-07-03 | 6
2 | 2016-07-01 | 8
2 | 2016-07-02 | 7

Mysql - Rows within hourly ranges on specific days using DAYOFWEEK

I am trying to get a query that will return a result set with reading total within certain hour ranges (defined in the working_hours table) depending on the DAYOFTHEWEEK for the date with a result that looks like:
working | nonworking | weekend | date | group_id
-----------------------------------------------------------------
50.3 | 30.8 | 0 | 2015-04-01 00:00 | 7
40.3 | 60.8 | 0 | 2015-04-01 00:00 | 8
50.3 | 30.8 | 0 | 2015-04-02 00:00 | 7
40.3 | 60.8 | 0 | 2015-04-02 00:00 | 8
Working and Weekend ranges are stored in the database in working_hours, Nonworking time ranges are implied (NOT BETWEEN the other ranges on that day basically)
The tables are as following:
Readings table has the hourly readings, named readings
group_id | reading | datestamp
------------------------------------------------------
7 | 30.8 | 2015-04-01 00:00
7 | 20.2 | 2015-04-01 01:00
7 | 11.2 | 2015-04-02 00:00
7 | 20.2 | 2015-04-02 01:00
8 | 26.2 | 2015-04-01 00:00
8 | 30.2 | 2015-04-01 01:00
8 | 26.2 | 2015-04-02 00:00
8 | 30.2 | 2015-04-02 01:00
Hour Ranges are stored in the working_hours table, the day column is DAYOFTHEWEEK format (1 = Sunday, 2 = Monday, etc):
group_id | day | range_start | range_end | range_type_id | day_type_id
------------------------------------------------------------------------------
7 | 5 | 08:00:00 | 15:59:00 | 1 | 1
7 | 6 | 00:00:00 | 05:59:00 | 1 | 2
7 | 6 | 06:00:00 | 23:59:00 | 2 | 2
7 | 1 | 00:00:00 | 22:59:00 | 2 | 4
7 | 1 | 23:00:00 | 23:59:00 | 1 | 4
Day Types are in the working_hours_day_type table and where things get complicated for me, Weekday and Weekend only have one range but Start/End Weekend have two ranges ('Start Weekend' first range is working hours, second range weekend hours and 'End Weekend' first range is weekend hours, second range working hours).
id | type
------------------
1 | Weekday
2 | Start Weekend
3 | Weekend
4 | End Weekend
Range Types are in the working_hours_range_type table:
id | type
------------------
1 | Working
2 | Weekend
My Mysql knowledge is limited to simple SELECT, INSERT etc and the basics of JOINs - I have found out about HOUR(datestamp) BETWEEN 8 AND 14 but dont know how to get subqueries to iterate within a parent query using WHERE datestamp BETWEEN '2015-04-01 00:00:00' AND '2015-04-02 23:59:00' if in fact thats how its done...
I am not totally clear on what constitutes working hours or non-working hours, but does this work?
SELECT
sum(CASE WHEN rtype.range_type_id = 1 THEN reading ELSE 0 END) AS working
sum(CASE WHEN rtype.range_type_id = 2 THEN reading ELSE 0 END) AS nonworking
CASE WHEN r1.daynum in (7,1) THEN 1 ELSE 0 END as weekend
date(datestamp) as date
r1.group_id
FROM
(SELECT
group_id,
reading,
time(datestamp) as rTime,
case when weekday(datestamp) = 0 THEN 2 #weekday() monday to working hours monday
when weekday(datestamp) = 1 THEN 3
when weekday(datestamp) = 2 THEN 4
when weekday(datestamp) = 3 THEN 5
when weekday(datestamp) = 4 THEN 6
when weekday(datestamp) = 5 THEN 7
when weekday(datestamp) = 6 THEN 1
else NULL
END CASE AS daynum
FROM readings) AS r1
LEFT JOIN working_hours w1
ON (r1.daynum = w1.day)
AND (r1.group_id = w1.group_id)
AND (r1.rTime BETWEEN w1.range_start AND w1.range_end)
LEFT JOIN working_hours_day_type dtype
ON w1.day_type_id = dtype.id
LEFT JOIN working_hours_range_type rtype
ON w1.range_type_id = rtype.id
GROUP BY
CASE WHEN daynum in (7,1) THEN 1 ELSE 0 END,
date(datestamp) as date,
r1.group_id

mysql Return amount of order for hour with on colum day of month

I have a mysql db which I use to return amounts of orders by hour in a specific day. I use this SELECT statement for that.
select
hour(datains),sum(valore)
from
ordini
where (stato=10 or stato = 1 ) and DATE(datains) = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 0 DAY)
group by hour(datains)
order by
id DESC
It returns:
+--------------+---------------+
| hour datains | valore |
| 12 | 34 |
| 11 | 56 |
| 10 | 134 |
+-------------------------------
Now I need to have columns for a certain number of days, like this.
+--------------+---------------+--------------+--------------+
| hour datains | 01-01-2014 | 02-01-2014 | 03-01-2014 |
| 12 | 34 | 34 | 77 |
| 11 | 56 | 0 | 128 |
| 10 | 134 | 66 | 12 |
+------------------------------+-----------------------------+
Is this possible?
It seems you have a table ordini with columns datains, valore, and stato.
Perhaps you can try this query to generate hour-by-hour aggregates for a three days' worth of recent sales, but not including today.
SELECT DATE_FORMAT(datains, '%Y-%m-%d %H:00') AS hour,
SUM(valore) AS valore
FROM ordini
WHERE (stato = 1 OR stato = 10)
AND datains >= CURRENT_DATE() - INTERVAL 3 DAY
AND datains < CURRENT_DATE
GROUP BY DATE_FORMAT(datains, '%Y-%m-%d %H:00')
ORDER BY DATE_FORMAT(datains, '%Y-%m-%d %H:00')
This will give you a result set with one row for each hour of the three days, for example:
2014-01-01 10:00 456
2014-01-01 11:00 123
2014-01-02 10:00 28
2014-01-02 11:00 350
2014-01-02 12:00 100
2014-01-02 13:00 17
2014-01-03 10:00 321
2014-01-03 11:00 432
2014-01-03 12:00 88
2014-01-03 13:00 12
That's the data summary you have requested, but formatted row-by-row. Your next step is to figure out an appropriate technique to pivot that result set, formatting it so some rows become columns.
It happens that I have just written a post on this very topic. It is here:
http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/