I want to find data for the last 20 Tuesday.
Date value
2020-03-03 01:12:15 5
2020-02-25 07:12:15 13
2020-02-24 08:12:15 1
2020-02-23 09:12:15 32
2020-02-22 10:12:15 7
2020-02-21 11:12:15 43
2020-02-20 12:12:15 7
2020-02-19 13:12:15 1
2020-02-18 14:12:15 31
2020-02-17 15:12:15 14
and so one
My desired output is
Date value
2020-03-03 01:12:15 5
2020-02-25 07:12:15 13
2020-02-18 14:12:15 31
and so on
Is this what you want?
select t.*
from t
where weekday(date) = 2
order by date desc
limit 3 -- or 20
You can use below query
select t.*
from t
where weekday(date) = 2
order by date desc
limit 20
You could use year and dayofweek
select * from myTable
where year(date) = 2020
and dayofweek(date) = 5
Related
Hello i have a table with some ids and values
for example:
SELECT instrumentid, value from `mytable` where instrumentid in (12,11, 14,15);
id, instrumentid, recorddate, value
33 12 2022-10-05 55
34 11 2022-10-05 33
30 14 2022-10-05 13
29 12 2022-10-03 12
28 11 2022-10-03 53
40 14 2022-10-03 4
44 15 2022-10-03 4
as result i want or better explained only the last newst entry for instrumentid
instrumentid, value
12 55
11 33
14 13
15 4
thanks and regards
running latest mariadb 10.9.3
You can use ROW_NUMBER() to identify the last row for each instrument.
For example:
select *
from (
select t.*,
row_number() over(partition by instrumentit order by recorddate desc) as rn
from mytable t
where instrumentid in (12,11, 14,15)
) x
where rn = 1
I am trying to self join in my current script in order to find the next row and then whatever day specified it should minus 1 day from it and put that in the end date column for the current row, but I seem to be going wrong somewhere.
SELECT
BCG.BudgetId
,B.CustomerId
,CAST(BCG.StartOfPeriod AS DATE) AS StartOfPeriod
,BCG2.EndOfPeriod
,ROUND(SUM(BCG.Charge),2) AS ExpenditureBudget
,ROUND(SUM(BCG.Consumption),2) AS ConsumptionBudget
,ROW_NUMBER() OVER (PARTITION BY BCG.BudgetId ORDER BY BCG.StartOfPeriod ASC) AS rowNum
,B.Status
FROM Budgets_BudgetCalcGroup BCG
INNER JOIN Budgets_Budget B ON B.Id = BCG.BudgetId
LEFT JOIN Budgets_BudgetCalcGroup BCG2 ON
BCG2.EndOfPeriod = (SELECT MIN(StartOfPeriod)-1
FROM Budgets_BudgetCalcGroup AS t3
WHERE t3.StartOfPeriod > t1.StartOfPeriod
)
WHERE B.Status = 2
GROUP BY BCG.BudgetId,StartOfPeriod
Error Received:
Unknown Column BCG2.EndOfPeriod in field list
Expected Output:
254 41 2018-09-01 2018-09-30 29017.8 542331.59 1 2
254 41 2018-10-01 2018-10-31 27858.82 575545.97 2 2
254 41 2018-11-01 2018-11-30 28927.71 576106.15 3 2
254 41 2018-12-01 NULL 34639.71 613779.57 4 2
I found an alternative way other than doing a self join which utilises the LEAD() function.
DATE_ADD(CAST(LEAD(BCG.StartOfPeriod, 1) OVER (PARTITION BY BCG.BudgetId ORDER BY BCG.StartOfPeriod) AS DATE),INTERVAL -1 DAY) AS EndOfPeriod
Output:
254 41 2018-09-01 2018-09-30 29017.8 542331.59 1
254 41 2018-10-01 2018-10-31 27858.82 575545.97 2
254 41 2018-11-01 2018-11-30 28927.71 576106.15 3
254 41 2018-12-01 2018-12-31 34639.71 613779.57 4
installment_table:
acc_no
installment_no
due_date
12
1
2022-04-22
12
2
2022-05-22
12
3
2022-06-22
12
4
2022-07-22
12
5
2022-08-22
transaction_table:
acc_no
txn_date
12
2022-04-22
12
2022-05-24
12
2022-06-18
12
2022-07-18
12
2022-08-25
resulting_table:
acc_no
installment_no
due_date
txn_date
days_diff
12
1
2022-04-22
2022-04-22
0
12
2
2022-05-22
2022-05-24
2
12
3
2022-06-22
2022-06-18
-4
12
4
2022-07-22
2022-07-18
-4
12
5
2022-08-22
2022-08-25
3
I want to join the tables based on the account number and the nearest transaction date. Is there a way to do it in MySQL 8.0.37?
You can use a lateral join to evaluate the days difference and select the minimum for each row:
select *
from installment i,
lateral(
select txn_date, DateDiff(txn_date, due_date) days_diff
from transaction t
where t.acc_no = i.acc_no
order by Abs(DateDiff(txn_date, due_date))
limit 1
)t;
See Example fiddle
I'm trying to group my data by 7 days interval.
for example.
I have a data which is you can find it below.
count startDate finish_date
1247 2017-03-09 08:43:18 2017-03-09 16:05:34
1681 2017-03-10 08:30:13 2017-03-10 16:31:55
1464 2017-03-11 08:36:50 2017-03-11 16:42:03
1343 2017-03-12 08:26:57 2017-03-12 16:39:58
1333 2017-03-13 08:35:34 2017-03-13 16:26:18
1215 2017-03-14 08:36:58 2017-03-14 16:13:20
1817 2017-03-16 08:24:49 2017-03-16 17:18:19
1675 2017-03-17 08:22:30 2017-03-17 16:36:58
1546 2017-03-18 08:33:52 2017-03-18 16:51:52
1443 2017-03-20 08:11:00 2017-03-20 16:26:38
1481 2017-03-21 08:26:04 2017-03-21 16:57:30
1574 2017-03-23 08:19:07 2017-03-23 16:12:46
1270 2017-03-24 08:25:25 2017-03-24 16:37:59
1765 2017-03-25 08:22:58 2017-03-25 16:44:24
1200 2017-03-26 08:37:47 2017-03-26 14:59:51
1479 2017-03-27 08:17:50 2017-03-27 15:18:32
And I wanted to group them by 7 days interval.
I tried this. for it.
select count(*), min(locationDate) as startDate, max(locationDate) as finish_date from location where tagCode = 24901 and xLocation >= 278 and xLocation <= 354 and yLocation >= 239 and yLocation <= 426 and locationDate
>= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and locationDate <= '2017-03-27 23:59:59' group by DATEDIFF(locationDate, '2017-03-01 00:00:01') div 7
And data is like.
count startDate finish_date
8283 2017-03-09 08:43:18 2017-03-14 16:13:20
7962 2017-03-16 08:24:49 2017-03-21 16:57:30
7291 2017-03-23 08:19:07 2017-03-27 15:22:05
Problem is Second Week it must start from 2017-03-15 and third week need to start 2017-03-22 but because of there is no data in on days its not starting how can I fix it ?
As I asked you in my comment, I think the result you wrote would be good with the input you provided, but it wouldn't be with a different input (like having 2017-03-15 but not 2017-03-16).
A solution could be to write the query kind of like this
select sum(count) as count, min(location_date), max(location_date)
from (
select t1.location_date,
t1.count,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start
from location t1
cross join
(select min(location_date) as min_date from location) t2
where t1.tagCode = 24901 and
t1.xLocation between 278 and 354 and
t1.yLocation between 239 and 426 and
t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and
t1.locationDate <= '2017-03-27 23:59:59'
) t3
group by week_start
I tested a simplified version of this on a simplified version of your input, there might be typos...
Edit
To display both interval starting date and ending date, try with this
select sum(count) as count, week_start, week_end
from (
select t1.count,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) day) week_start,
date_sub(location_date, interval (datediff(t1.location_date, t2.min_date) % 7) - 6 day) week_end
from location t1
cross join
(select min(location_date) as min_date from location) t2
where t1.tagCode = 24901 and
t1.xLocation between 278 and 354 and
t1.yLocation between 239 and 426 and
t1.locationDate >= DATE_SUB('2017-03-01 00:00:01',INTERVAL 7 day) and
t1.locationDate <= '2017-03-27 23:59:59'
) t3
group by week_start, week_end
I just use GROUP BY DATE_FORMAT:
SELECT someTimeStamp,SUM(amount) AS Total FROM sometable WHERE 1 GROUP BY DATE_FORMAT(someTimeStamp,"%Y%v")
I think you can do that :
you need to change the result of your query from this :
1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32
to This using the logic of computing the number of days between the max date and the min date of the first line :
-- max date of the row min date of the first row
select FLOOR(datediff('2017-03-12 16:05:34', '2017-03-09 08:43:18')/7);
select FLOOR( datediff('2017-03-16 17:18:19', '2017-03-09 08:43:18')/7);
-- what is important that you always compute the max date - the min date of the first row the same row like in your example is : '2017-03-09 08:43:18'
select FLOOR( datediff(max_date, '2017-03-09 08:43:18')/7);
rec_sum min_date max_date day_diff
1 1247 2017-03-09 08:43:18 2017-03-09 16:05:34 0
2 1681 2017-03-10 08:30:13 2017-03-10 16:31:55 0
3 1464 2017-03-11 08:36:50 2017-03-11 16:42:03 0
4 1343 2017-03-12 08:26:57 2017-03-12 16:39:58 0
5 1333 2017-03-13 08:35:34 2017-03-13 16:26:18 0
6 1215 2017-03-14 08:36:58 2017-03-14 16:13:20 0
7 1817 2017-03-16 08:24:49 2017-03-16 17:18:19 1
8 1675 2017-03-17 08:22:30 2017-03-17 16:36:58 1
9 1546 2017-03-18 08:33:52 2017-03-18 16:51:52 1
10 1443 2017-03-20 08:11:00 2017-03-20 16:26:38 1
11 1481 2017-03-21 08:26:04 2017-03-21 16:57:30 1
12 1574 2017-03-23 08:19:07 2017-03-23 16:12:46 2
13 1270 2017-03-24 08:25:25 2017-03-24 16:37:59 2
14 1765 2017-03-25 08:22:58 2017-03-25 16:44:24 2
15 1200 2017-03-26 08:37:47 2017-03-26 14:59:51 2
16 1479 2017-03-27 08:17:50 2017-03-27 15:18:32 2
-- now you can group the new result by the new field the result of division.
select
sum(rec_sum) ,
min(min_date),
max(max_date)
from (query result in the previous list)
group by day_diff
i know it's a little bit hard but i think you can do it the hard way is the day_diff computing .
My table votes contains votes that have been made by users at different times:
id item_id position user_id created_at
1 2 0 1 11/21/2013 11:27
26 1 1 1 11/21/2013 11:27
27 3 2 1 11/21/2013 11:27
42 2 2 1 12/7/2013 2:20
41 3 1 1 12/7/2013 2:20
40 1 0 1 12/7/2013 2:20
67 2 2 1 12/13/2013 1:13
68 1 1 1 12/13/2013 1:13
69 3 0 1 12/13/2013 1:13
84 2 0 1 12/28/2013 2:29
83 3 2 1 12/28/2013 2:29
82 1 1 1 12/28/2013 2:29
113 3 0 1 1/17/2014 22:08
114 1 1 1 1/17/2014 22:08
115 2 2 1 1/17/2014 22:08
138 2 0 1 1/20/2014 16:49
139 1 1 1 1/20/2014 16:49
140 3 2 1 1/20/2014 16:49
141 1 1 11 1/20/2014 16:51
142 3 2 11 1/20/2014 16:51
143 2 0 11 1/20/2014 16:51
I need to tally the results on a monthly basis but here's the tricky part: the start/end of the month does not necessarily fall on the first day of the month. So if the votes are due on the 10th day of every month, I need a vote that was cast on the 10th to be in a different group from a vote that was cast on the 11th. Using the data above, I want to get three groups:
Group 1: 6 votes (11/21 and 12/7)
Group 2: 6 votes (12/13, 12/28)
Group 3: 9 votes (1/17, 1/20)
I've tried a lot of approaches but to no avail. This is my query right now:
select created_at, ADDDATE(DATE_FORMAT(created_at, '%Y-%m-01'),interval 10 day) as duedate,count("id") from votes where list_id = 2 group by duedate
I am getting group sizes of 3, 9, and 9, not 6, 6 and 9. Any help you can provide would be much appreciated. Thanks.
Your query is close. You just need to subtract 9 days (10 - 1) from the current day to get the month:
select created_at, date_sub(created_at, interval 9 day) as duedate,
count(id)
from votes
where list_id = 2
group by duedate;
date_format() converts a date to a string. There is no need to convert a date value to a character value for this query.
EDIT:
To group by month:
select date_format(date_sub(created_at, interval 9 day), '%Y-%m') as YYYYMM,
count(id)
from votes
where list_id = 2
group by YYYYMM;