How to select DATE related to MAX value column in MySQL - mysql

I have a table tbl with column date sales
I perform this MySQL query:
SELECT DAY(date) as DAY, count(sales) as sales_count
FROM tbl
I get this result:
DAY sales_count
1 224
2 361
3 155
4 281
.......
30 416
31 332
Now, I find the DAY in WEEK when the sales were MAX by doing this:
SELECT DAY(date) as DAY, WEEK(date) as WEEK, max(count(sales)) as MAX_SALES,
FROM tbl
GROUP BY WEEK(date)
and I get this:
DAY WEEK MAX_SALES
1 1 324
8 2 461
15 3 255
22 4 481
.......
The WEEK and MAX_SALES are correct but the DAY value seems to be the first DAY of the WEEK and not the DAY of the MAX_SALES occurred. I can confirm it is because we have GROUP BY WEEK and that is how DAY is getting the value related to WEEK. We cannot group by MAX()
How do I have the DAY value reflect the MAX_SALES date in the WEEK?

You can try to use subquery.
SELECT weekDay,
DAY,
MAX(sales_count)
FROM (
SELECT DAY(date) as DAY,
Week(date) as weekDay
count(sales) as sales_count
FROM tbl
GROUP BY
DAY(date),
Week(date)
) t1
group by
weekDay,
DAY

Related

How to know the date interval covered by WEEK function

I want to do other 2 columns that shows me, respectively, the first date and the last day of the week grouped.
Currently, I'm summarizing a query by week period. When I run this query, it shows me the results on table:
SELECT
pc.date,
CONCAT(YEAR(pc.date), '/', WEEK(pc.date)) as year_week
FROM pc
GROUP BY CONCAT(YEAR(pc.date), '/', WEEK(pc.date))
ORDER BY pc.date
date
year_week
2020-09-02
2020/35
2020-09-07
2020/36
2020-09-17
2020/37
2020-09-23
2020/38
2020-09-28
2020/39
2020-10-10
2020/40
2020-10-11
2020/41
2020-10-21
2020/42
2020-10-28
2020/43
How can I find the first and last day of grouped week?
You can use the WEEKDAY function. Demo:
select
date_add(dt, interval -WEEKDAY(dt)-1 day ) FirstDayOfWeek,
date_add(date_add(dt, interval -WEEKDAY(dt)-1 day), interval 6 day) LastDayOfWeek,
week(dt) wk
from (
select '2020-09-02' dt union all
select '2020-09-07' union all
select '2020-09-17'
) t
Returns
FirstDayOfWeek LastDayOfWeek wk
2020-08-30 2020-09-05 35
2020-09-06 2020-09-12 36
2020-09-13 2020-09-19 37

MySQL query to fetch row count of hourly records inserted and max record per minute in that hour

I have the table with following fields
id CreatedOn(yyyymmddhhmmss) Channel_id
10 20200617160916792 13
11 20200617160919792 13
12 20200617170919792 13
13 20200617170919792 13
14 20200617160920212 14
I need a query to check how many total records are inserted in particular hour and maximum records per minutes in that hour based on channel Id. For example if 100 record are inserted at 15:00-16:00 then what is maximum record insert per minutes. Please help me creating a query to find the required solution
I have run this query, Although it is given me total record per hour but I also want to know how maximum are inserted per min.
SELECT DATE_FORMAT(CreatedOn, '%H:00') as hours,
count(CreatedOn) as total_hit, channel_id
FROM tbl_transaction_flow where DATE_FORMAT(CreatedOn, '%Y-%m-%d') = DATE(NOW())
GROUP BY DATE_FORMAT(CreatedOn, '%Y-%m-%d %H:00'),channel_id;
Need output as
hours total_hits channel_id max_hit_per_minutes
16:00 100 13 20
17:00 10 13 1
WITH cte AS ( SELECT DATE_FORMAT(CreatedOn, '%H:%i') as hours_minutes,
COUNT(CreatedOn) as hits_per_minute,
Channel_id
FROM tbl_transaction_flow
WHERE DATE(CreatedOn) = /* CURRENT_DATE */ '2020-06-17'
GROUP BY hours_minutes, Channel_id )
SELECT DISTINCT
CONCAT(LEFT(hours_minutes, 2), ':00') hours,
Channel_id,
SUM(hits_per_minute) OVER win hits_per_hour,
MAX(hits_per_minute) OVER win max_hits_per_minute
FROM cte
WINDOW win AS (PARTITION BY Channel_id, LEFT(hours_minutes, 2));
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b2635180061e45e761864ff7b4b018f3
SELECT CONCAT(LEFT(hours_minutes, 2), ':00') hours,
Channel_id,
SUM(hits_per_minute) hits_per_hour,
MAX(hits_per_minute) max_hits_per_minute
FROM ( SELECT DATE_FORMAT(CreatedOn, '%H:%i') as hours_minutes,
COUNT(CreatedOn) as hits_per_minute,
Channel_id
FROM tbl_transaction_flow
WHERE DATE(CreatedOn) = /* CURRENT_DATE */ '2020-06-17'
GROUP BY hours_minutes, Channel_id ) per_minute_data
GROUP BY hours, Channel_id;
https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=d7f14899377fc491efb4ca8635e4a4a6
SELECT date_format(created_at,'%H %p') AS hour, count(*) AS total_record
FROM users
WHERE CAST(created_at AS DATE) ="2021-12-15"
GROUP BY date_format(created_at,'%H %p')

how to get pending months from due entries using mysql

I have the entries of monthly dues like,
Table name : month_dues,
Columns:
customer_id,
due_date,
due_amount
These table have lot of due entries.
customer_id due_date due_amount
--------------------------------------
1 2018-12-01 100
1 2019-01-01 100
1 2019-02-01 100
1 2019-10-01 100
so, how to select pending due months from these record?
in my table customer 1 not paid dues for these months,
2019-03,2019-04,2019-05,2019-06,2019-07,2019-08, 2019-09
customer pay the due every month so
For select paid dues from table using,
SELECT customer_id, due_date, due_amount FROM month_dues where customer_id='1' where due_date>='2019-01-01' and due_date<='2019-10-18';
How to get pending due month and year using this table?
Which means, find month and year not in this record.
IF you are searching between the two dates
YOu can use this
select * from month_dues
where due_date between '2012-03-11 00:00:00' and '2012-05-11
23:59:00' && customer_id='1'
order by month_dues desc;
If you want to search the date(due_date) lower than today
SELECT * FROM month_dues WHERE due_date < CURDATE();
And you are refeering to the record that is not belong to the query you can find some NOT IN query
Like
`SELECT * FROM month_dues NOT IN ( select * from month_dues
where due_date between '2012-03-11 00:00:00' and '2012-05-11
23:59:00' && customer_id='1'
order by month_dues desc;
)`
So basically you need to validate your table against some kind of calendar, here is a simple solution that only works for the current year, maybe you can use it as a start or someone else could improve it
SELECT m.MONTH
FROM (SELECT 1 AS MONTH
UNION SELECT 2 AS MONTH
UNION SELECT 3 AS MONTH
UNION SELECT 4 AS MONTH
UNION SELECT 5 AS MONTH
UNION SELECT 6 AS MONTH
UNION SELECT 7 AS MONTH
UNION SELECT 8 AS MONTH
UNION SELECT 9 AS MONTH
UNION SELECT 10 AS MONTH
UNION SELECT 11 AS MONTH
UNION SELECT 12 AS MONTH) as m
WHERE m.MONTH NOT IN (SELECT MONTH(due_date)
FROM due_months
WHERE customer_id = 1
AND YEAR(due_date) = YEAR(CURDATE()))
AND m.MONTH < MONTH(CURDATE()) -- needs to be improved as well

query to find count of records on daily basis for given time range

I need to count no of records in a table daily between 9 AM to 6 PM on a given date range.
Sample output should look like:
Date Count of users
01-11-2018 100
02-11-2018 88
03-11-2018 107
04-11-2018 113
SELECT DATE(date), count(*)
FROM table
WHERE TIME(date) BETWEEN TIME('9:00:00') AND TIME('18:00:00')
GROUP BY DATE(date)
SELECT `date`,
Count(*)
FROM table_name tn
WHERE tn.date >= Timestampadd(hour, 9, Curdate())
AND tn.date <= Timestampadd(hour, 18, Curdate())
GROUP BY `date`

Sql Query - group by month from 1st day up todays date

I have a record set of sales amount daily with different branches.
For example
Date Amount Branch
01/01/2014 30 A
01/01/2014 30 B
01/02/2014 40 A
01/02/2014 40 B
01/03/2014 30 A
01/03/2014 30 B
up to feb,mar,apr,may,jun,jul,aug
What i want to achieve is to group the record monthly based on todays date day.
For example today is 08/11/2014. the range should be 1st day of the month "1" then i will pick the day today which is 11. So the range for all the months is 1-11. See below sample.
Date Range for query monthly
01/01/2014-01/11/2014
02/01/2014-02/11/2014
03/01/2014-03/11/2014
04/01/2014-04/11/2014
05/01/2014-05/11/2014
06/01/2014-06/11/2014
07/01/2014-07/11/2014
08/01/2014-08/11/2014
Group this date range and get the sum of total sales.
Please help
This should do most of the work:
SELECT MONTH(date), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date);
UPDATE
For the 3 letter month tag, also you'll probably want an ORDER BY to be sure:
SELECT DATE_FORMAT(date,'%b'), SUM(amount)
FROM table_name
WHERE DAY(date) <= DAY(CURDATE())
AND date >= YEAR(CURDATE())
GROUP BY MONTH(date)
ORDER BY MONTH(date);
You should be able to achieve what you want by using the following MySQL query:
select sum('amount') from 'some_table'
where dayofmonth('sell_date') >= 1
and dayofmonth('sell_date') < dayofmonth(currdate())
group by month('sell_date');
I hope it works, did not have some database to test.
You could eventually also group by branch, by adding an additional , 'branch' before the query's semicolon.