group by month depend on last date in month - mysql

I want to make a query to group date by month depend on the last date.
in my table:
july
2015-07-02
2015-07-03
august
2015-08-04
this is my query:
SELECT DISTINCT Visit_Date FROM tr_visit
JOIN tm_child ON tm_child.Child_ID = tr_visit.Child_ID
WHERE tr_visit.Child_ID='CH001'
GROUP BY YEAR(tr_visit.Visit_Date), MONTH(tr_visit.Visit_Date) DESC
My result is:
2015-07-02
and 2015-08-04
How can I select the last visit in july so the result become:
2015-07-03 (last visit in july)
and 2015-08-04 (last visit in august)

like this?
select max(visit_date)
from tr_visit
group by year(visit_date), month(visit_date);

Related

Mysql data fetch getting wrong time

i have some records on mysql db, i tried to group by month, data is correct but time of March month is showing as "2022-03-22", i required as same as other months like 2022-03-01.
time month_name inc_number
2022-01-04 19:58:09 January 39393
2022-02-08 17:36:33 February 90203
2022-03-22 13:40:48 March 82923
2022-04-01 00:14:33 April 23333
2022-05-01 00:31:58 May 33322
2022-06-06 17:21:29 June 33244
2022-07-01 04:19:20 July 90283
2022-08-01 00:07:04 August 8428
2022-09-01 09:40:15 September 10097
2022-10-01 00:30:19 October 6421
2021-12-01 07:12:30 December 8521
the query im using is below
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY MONTH(created_on)
ORDER BY MONTH(created_on)
Please suggest the way to get all time should be first date of each month.
If you use GROUP BY and do not specify a column, MySQL will just use one of the created_on values from the 'array'.
Instead you should define the expected output of created_on and add it to your GROUP BY
You could use something like DATE_FORMAT(created_on, '%Y-%m-01') to always display the first day of that month
working, thanks #verhie
SELECT
created_on as 'time',
MONTHNAME(created_on) AS 'month_name',
count(distinct id_number) AS "inc_number"
FROM test_reports
WHERE
MONTH(created_on)
GROUP BY DATE_FORMAT(created_on, '%Y-%m-01')
ORDER BY MONTH(created_on)

group by date containing certain date only

I have tried looking at some similar examples like group by date range and weekdays etc but I couldnt fix it on my query.
as per my sample data screenshot, I need to only return
sum(salesamount)/sum(salescount) for week 1
and
sum(salesamount)/sum(salescount) for week 2.
Each of the week contain 5 days (in this example is wednesday - sunday).
My Attempt:
select salesstartdate, date_add(salesstartdate, interval 5 day) as gdate,
salesamount, salescount, sum(salesamount)/sum(salescount) as ATV
from testing
group by gdate;
My desired output is:
Week 1 15.34173913
Week 2 15.80365088
Calculation to get week 1 is (3507.1+3639.97+5258.77+8417.04+5994.48)/(285+273+344+478+368)
Calculation to get week 2 is the same as above except the date would now be from 8 to 12 of June.
You can do it with a subquery. In order to first group your result set properly and then execute aggregation on it:
SELECT
concat('WEEK', ' ', weekno) as `Week #`,
MIN(salesstartdate) as startDate,
MAX(salesstartdate) as endDate,
sum(salesamount)/sum(salescount) as ATV
FROM
(
SELECT
salesstartdate,
salesamount,
salescount,
WEEKOFYEAR(salesstartdate) as weekno -- get the week number of the current year
FROM
weekno
WHERE
WEEKDAY(salesstartdate) BETWEEN 2 AND 6 -- get index of week day
) as weeks
GROUP BY
weekno
I have used 2 MySQL functions here:
WEEKOFYEAR()
WEEKDAY()
Output:
WEEK 23 | 2016-06-08 | 2016-06-12 | 15.8040
WEEK 24 | 2016-06-16 | 2016-06-19 | 15.9323
and without subquery as well:
SELECT
concat('WEEK', ' ', WEEKOFYEAR(salesstartdate)) as `Week #`,
MIN(salesstartdate) as startDate,
MAX(salesstartdate) as endDate,
sum(salesamount)/sum(salescount) as ATV
FROM
weekno
WHERE
WEEKDAY(salesstartdate) BETWEEN 2 AND 6 -- get index of week day
GROUP BY
WEEKOFYEAR(salesstartdate)
You can do this way
select SUBDATE(salesstartdate, WEEKDAY(salesstartdate)) as week_range
, sum(salesamount)/sum(salescount)
from testing
where salesstartdate between SUBDATE(salesstartdate, WEEKDAY(salesstartdate))
and date_add(SUBDATE(salesstartdate, WEEKDAY(salesstartdate)), interval 5 day))
Group by week_range

SQL order by clause for 2 date columns

I have a table with a column named timestamp
i want to be able to order by month(timestamp) and year(timestamp) and group the months and years together
so for example, if i had the following timestamps:
2014-01-01
2014-02-01
2014-05-01
2015-01-01
i want to show in this order
MONTH YEAR
1 2015
5 2014
2 2014
1 2014
You can pick month and year from timestamp with MONTH and YEAR:
GROUP BY MONTH(field_with_ts) , YEAR(field_with_ts)
and the same thing with the ORDER BY clause.
Try this:
substring(date, 8 , 2) as Month, substring(date, 1, 3) as Year
See, if that works.

MySQL Selecting MAX total with date and ID

I have a cron script that writes the total number of active users to a table every day. I'm trying to now generate a simple report that would show the "high water mark" for each month. Because some accounts expire during the month it's possible the highest number may NOT be at the end of the month.
Here's a sample of my table structure
tblUserLog
-----------
record_id INT(11) // PRIMARY KEY
run_date DATE // DATE RUN
ttl_count INT(11) // TOTAL FOR DAY
Sample data:
record_id run_date ttl_count
1 2013-06-01 500
2 2013-06-10 510
3 2013-06-20 520
4 2013-06-30 515
5 2013-07-01 525
6 2013-07-10 530
7 2013-07-20 540
8 2013-07-31 550
9 2013-08-01 560
What I would like returned is:
record_id run_date ttl_count
3 2013-06-20 520
8 2013-07-31 550
9 2013-08-01 560
I've tried two queries that are close...
// This will give me the total for the first of the month
SELECT s.record_id, s.run_date, s.ttl_count
FROM tblStatsIndividual s
JOIN (
SELECT record_id
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
HAVING MAX(ttl_count)
) s2
ON s2.record_id = s.record_id
ORDER BY run_date DESC
This returns the total for the first of each month, along with the record_id and correct date for the total.
Tried this...
SELECT record_id,max(run_date), max(ttl)
FROM (
SELECT record_id,run_date, max(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) a
GROUP BY DATE_FORMAT(run_date, '%Y %m')
ORDER BY run_date DESC
This one appears to get the correct "high water mark" but it's not returning the record_id, or the run_date for the row that IS the high water mark.
How do you get the record_id and the run_date for the highest total?
Something like
Select detail.Record_ID, detail.Run_Date, detail.ttl_Count
From tblStatsIndividual detail
Inner Join
(Select Year(run_date) as Year, Month(Run_date) as Month, Max(ttl_count) as ttl
From tblStatsIndividual
Group By Year(run_date), Month(Run_date)) maximums
On maximums.Year = Year(detail.Run_date) and maximums.Month = Month(detail.Run_date)
and maximums.ttl = detail.ttl_count
Should do it. NB based on your requirement if you had two records in the same month with the same (and highest in the month) ttl_count, they would both be returned.
Based on the help from #Tony Hopkinson, This query gets me the info. The one caveat is it shows the ID and date for the first occurrence of the MAX total, so if the total is the same three days in a row on a month, the first day's ID is returned. For my purpose, the last ID would be more ideal, but I can live with this:
SELECT s.Record_ID, s.Run_Date, s.ttl_Count
FROM tblStatsIndividual s
INNER JOIN (
SELECT YEAR(run_date) AS yr, MONTH(run_date) AS mon, MAX(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) maximums
ON maximums.yr = YEAR(s.run_date)
AND maximums.mon = MONTH(s.run_date)
AND maximums.ttl = s.ttl_Count
GROUP BY ttl_count
ORDER BY run_date DESC

MySQL distinct and left, right

I have a table with day column like this:
2011-04-28, 2011-04-29 ...
day count name surname
2011-04-28 8 titi tutu
2011-04-28 12 tutu toto
2011-04-27 2 tutu toto
2011-03-12 10 tutu toto
I can obtain distinct day but not only month and year.
select distinct(day) from Table where day between "2011-03-01" and "2011-04-28";
I want only distinct month and year.
Can you help me?
Thanks
select DISTINCT EXTRACT(YEAR_MONTH FROM `day`) as yearmonth
from Table
where day between '2011-03-01' and '2011-04-28';
DISTINCT may be applied only to the whole row in mysql. So, you need to extract what you need first from the date.
select distinct(EXTRACT YEAR_MONTH FROM `day`) from Table
where day between "2011-03-01" and "2011-04-28";