How to display the last one month result in where clause statement in DB2.
This is my query:-
SELECT distinct
payment_date dated_daily
FROM payment BB
WHERE YEAR(payment_date)= '2018'
AND MONTH(payment_date) = '04'
Say I have the following Column
dated_daily
2018-04-01
until
2018-04-30
After subtract
dated_daily
2018-03-01
until
2018-03-31
Anyone can help me?. Thank you
You can use NEXT_MONTH and FIRST_DAY to find out when a specific month starts. Moreover, Db2 supports date and time arithmetics. Just add or subtract days, weeks, months, years...
values(next_month('2018-04-16') - 1 day)
=> 2018-04-30
Related
I have a MySQL requirement to select data from a table based on a start date and end date and group it by weekly also selecting the data in reverse order by date. Assume that, I have chosen the start date as 1st November and the end date as 04 December. Now, I would like to fetch the data as 04 December to 28 November, 27 November to 20 November, 19 November to 12 November and so on and sum the value count for that week.
Given an example table,
id
value
created_at
1
10
2021-10-11
2
13
2021-10-17
3
11
2021-10-25
4
8
2021-11-01
5
1
2021-11-10
6
4
2021-11-18
7
34
2021-11-25
8
17
2021-12-04
Now the result should be like 2021-12-04 to 2021-11-28 as one week, following the same in reverse order and summing the column value data for that week. I have tried in the query to add the interval of 7 days after the end date but it didn't work.
SELECT count(value) AS total, MIN(R.created_at)
FROM data_table AS D
WHERE D.created_at BETWEEN '2021-11-01' AND '2021-12-04' - INTERVAL 7 DAY ORDER BY D.created_at;
And it's also possible to have the last week may have lesser than 7 days.
Expected output:
end_interval
start_interval
total
2021-12-04
2021-11-27
17
2021-11-27
2021-11-20
34
2021-11-20
2021-11-13
4
2021-11-13
2021-11-06
1
2021-11-06
2021-10-30
8
2021-10-30
2021-10-25
11
Note that the last week is only 5 days depending upon the selected from and end dates.
One option to address this problem is to
generate a calendar of all your intervals, beginning from last date till first date, with a split of your choice, using a recursive query
joining back the calendar with the original table
capping start_interval at your start_date value
aggregating values for each interval
You can have three variables to be set, to customize your date intervals and position:
SET #start_date = DATE('2021-10-25');
SET #end_date = DATE('2021-12-04');
SET #interval_days = 7;
Then use the following query, as already described:
WITH RECURSIVE cte AS (
SELECT #end_date AS end_interval,
DATE_SUB(#end_date, INTERVAL #interval_days DAY) AS start_interval
UNION ALL
SELECT start_interval AS end_interval,
GREATEST(DATE(#start_date), DATE_SUB(start_interval, INTERVAL #interval_days DAY)) AS start_interval
FROM cte
WHERE start_interval > #start_date
)
SELECT end_interval, start_interval, SUM(_value) AS total
FROM cte
LEFT JOIN tab
ON tab.created_at BETWEEN start_interval AND end_interval
GROUP BY end_interval, start_interval
Check the demo here.
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)
I have the following query to get the monthly amount of users:
SELECT count(user_id) from subs
where (started_at between #start_date and #start_date + interval 1 month
or (expires_at>#start_date + interval 1 month and started_at<#start_date))
If we had the following DB:
user_id started_at expires_at
=============================
1 2015-01-01 2015-12-31
2 2015-01-01 2015-01-03
3 2015-02-01 2015-02-28
4 2015-03-01 2015-03-31
5 2015-04-01 2015-04-31
6 2015-04-01 2016-04-01
7 2015-05-01 2015-05-09
I need a query that will return the following table:
2015-01 - 2
2015-02 - 2 (because one of Jan records doesn't expire till Dec)
2015-03 - 2
2015-04 - 3
2015-05 - 3
etc
So what is the efficient way to get this result in one query?
You probably want something like this:
SELECT YEAR(started_at) as 'Year',
MONTH(started_at) as 'Month',
COUNT(user_id) as 'Users'
FROM subs
GROUP BY YEAR(started_at),MONTH(started_at);
Note that in case a month has no users, this query will not return an entry for that month. If you want to also include months with 0 users you want a more complex query; check this for more info.
You want to GROUP BY the year and month.
Assuming your started_at column is of a DATE type, you can f.e. use GROUP_BY YEAR(started_at), MONTH(started_at), or also use DATE_FORMAT to format the column value to a single string value, of the form YYYY-MM and GROUP BY that. Select that same value as a column too, to get the proper identifier you want.
I'm trying to make a query to get rush hours for everyday on a specific month.
The table I have looks like this:
id idproduct created_at
1 021354684 2011-10-01 20:25:48
2 033546835 2011-10-01 20:30:15
3 055965654 2011-10-01 20:45:20
4 012975343 2011-10-02 14:03:36
5 021354684 2011-10-02 15:55:48
6 033546835 2011-10-02 16:30:15
7 055965654 2011-10-02 16:45:20
8 012975343 2011-10-02 18:53:36
9 021354684 2011-10-03 08:55:48
10 033546835 2011-10-03 09:30:15
11 055965654 2011-10-03 14:03:20
12 012975343 2011-10-03 14:03:36
What I try to get is something like this...:
day rush_hour number_of_rows
1 20:00 3
2 16:00 5
3 14:00 4
Is it possible to get a table like this? can you guys help me?
I made a mistake, sorry for this. The number of rows should be the total of items sold that day, not in that hour :( sorry.
http://sqlfiddle.com/#!2/5b87b/7
First, count every day's every hour's count (into a view, because we will use it twice below):
CREATE VIEW hours AS
SELECT
DATE( created_at ) AS d,
HOUR( created_at ) AS h,
COUNT(*) AS c
FROM item
GROUP BY DATE(created_at), HOUR(created_at);
Final query:
SELECT
hours.d AS `day`,
hours.h AS `rush_hour`,
hours.c AS `count`
-- get the max count for every day
FROM (
SELECT
d, -- the day
MAX(c) as c -- the count
FROM hours
GROUP BY d
) AS maxc
-- find the actual hour(s) with the max count for every day:
INNER JOIN hours ON hours.c = maxc.c
AND hours.d = maxc.d;
You're going to want to look at the MySQL Date Functions, they offer you some help with this
SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
http://sqlfiddle.com/#!2/62a15/2/0
Try this:
SELECT dayofyear(created_at) as day, hour(created_at) as rush_hour, count(*) as number_of_rows
FROM table
GROUP BY dayofyear(created_at), hour(created_at);
Here it is without making a view:
SELECT ddd.day, eee.rush_hour, ddd.maxo
FROM
(select day, max(num_rows) as maxo from (
SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
) as groupo group by day) as ddd
LEFT JOIN
(SELECT
day(created_at) as day,
hour(created_at) as rush_hour,
count(1) as num_rows
FROM item
GROUP BY
day(created_at), hour(created_at)
) as eee on ddd.day=eee.day and ddd.maxo=eee.num_rows
I could imagine it being formatted more nicely or having more relevant aliases, but there's just so much subselecting going on here.
And thanks SQLfiddlers for putting the data there.
And I think that if you have two hours tied for the highest number of whatever it is you are counting, they both will show up, so you'll get two (or more) records returned for that day of the month.
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";