SELECT
timestamp,
name,
count(b.name)
FROM time a, id
WHERE …
GROUP BY
UNIX_TIMESTAMP(timestamp) DIV 300, name
I have above table
and want to count created_at column
group by 5 minutes interval of created_at ,
like count create_at column
from 2016-09-01 16:21:29 to 2016-09-01 16:26:29
You have to use FLOOR() function also for get unique integer for every five minute
SELECT timestamp, name, count(b.name) FROM time a, id WHERE … GROUP BY FLOOR(UNIX_TIMESTAMP(timestamp)/300)
Related
I'm trying to figure out how to select the latest DateTime value in a table and all previous data that came with it in a 30 minute window using MySQL.
eg table:
DateAndTime
---------
09:00:00
08:59:50
08:59:40
08:59:30
08:59:20
08:59:10
08:59:00
08:58:50
08:59:40
...
...
08:30:00
I am selecting max time as such:
SELECT MAX(`DateAndTime`) AS "DateAndTime"
FROM TableA;
I have been trying the INTERVAL() function, but I can't seem to get that to return any other rows other than the max time.
What I tried:
SELECT MAX(`DateAndTime`) AS "DateAndTime"
FROM TableA;
AND `DateAndTime` - INTERVAL 30 MINUTE;
We can use your query as subquery in the WHERE clause:
SELECT DateAndTime
FROM tableA
WHERE DateAndTime >=
(SELECT MAX(DateAndTime) - INTERVAL 30 MINUTE
FROM tableA);
If we want to select further columns, we will just add them in the main query.
If we want to make sure the result will be sorted by date, we will add an ORDER BY clause:
SELECT DateAndTime
FROM tableA
WHERE DateAndTime >=
(SELECT MAX(DateAndTime) - INTERVAL 30 MINUTE
FROM tableA)
ORDER BY DateAndTime;
It looks like you'll need a subquery because you need an aggregate function to find the latest timestamp, then to use that value to return the rows you need. Try this:
SELECT DateAndTime
FROM TableA a
WHERE DATE_ADD(a.DateAndTime, INTERVAL 30 MINUTE) >=
(SELECT MAX(DateAndTime) FROM TableA)
I have a mysql table table_a, it has fields like date(varchar), user_id, and others, I create a multicolumn index index_date_user_id (date, user_id). I want to iterate over the table with paging, like
select * from table_a where date < '20210304' and date > '20210108'
order by date, user_id limit 4 offset 0
select * from table_a where date < '20210304' and date > '20210108'
order by date, user_id limit 4 offset 4
select * from table_a where date < '20210304' and date > '20210108'
order by date, user_id limit 4 offset 8
select * from table_a where date < '20210304' and date > '20210108'
order by date, user_id limit 4 offset 12
I want there are no overlapping/duplicate rows between these paged rows. But since there is a range condition for column "date", the second column of the index_date_user_id does't work. If I force the index, like
select * from table_a force index (index_date_user_id)
where date < '20210304' and date > '20210108'
order by date, user_id limit 4 offset 0
since the ordering in index_date_user_id is by (date, user_id), does order by date, user_id work? e.g, the ordering is like
('20210109', 1111), ('20210109', 1112),('20210109', 1113),
('20210302', 999),('20210303', 14)
where values like '20210108' are date, while 1111, 1112, 14 are user_id
besides, how to optimize if the m is very big for limit n offset m
I have an invoice table and I want to generate the date range from two-column based on the third column(as frequency). Or you can say that I want to check the current date between date ranges but each date range is different for each row.
As you can see in the picture, I have the start date, end date, and frequency. The frequency is in the months. I have two rows and I want to get all the dates that lie between two dates column by adding the frequency(third column) as a month but should be less than or equal to the end_date column.
So I want the date range like below
2020-11-23
2020-12-23
2021-01-23
2021-02-23
2021-03-23
2021-04-23
2021-05-23
because of start_date:- 2020-11-23, end_date:- 2021-06-21, and frequency = 1 in first row
and another date range like below
2020-11-19
2021-01-19
2021-03-19
because of start_date:- 2020-11-19, end_date:- 2021-03-20, and frequency = 2 in second row
So at last, I need to check today's date in the above ranges like
SELECT
id
FROM `invoices`
where CURRENT_DATE in(
'2020-11-23', '2020-12-23', '2021-01-23', '2021-02-23', '2021-03-23', '2021-04-23', '2021-05-23', '2020-11-19', '2021-01-19', '2021-03-19')
WITH RECURSIVE
cte AS ( SELECT id, start_date, end_date, frequency
FROM test
UNION ALL
SELECT id, start_date + INTERVAL frequency MONTH, end_date, frequency
FROM cte
WHERE start_date <= end_date )
SELECT id, start_date `date`
FROM cte
WHERE start_date <= end_date
ORDER BY 1,2
fiddle
I have id, date,time(doenst have date) columns, and I am just wondering how to select rows where id repeated at least two other times(repeated three times) in last hour..
enter image description here
use GROUP BY query to get duplicate ID
select *
from 'table'
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR)
group by id
having count(id) >= 2
select * from 'table' where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR) group by id having count(id) >= 2
Will select records within last hour, group them by id and filter which group have more than 1 records
I need to get the sales total from our table for a certain date range, and display the results grouped by day of the week. (making a chart)
The following is what i have so far
SELECT DAYNAME(sale_date), SUM(total_price + total_tax)
FROM threewor_vend.vendsales AS s
WHERE s.register_id = '07709f8e-8d90-11e0-8e09-4040f540b50a'
AND sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
GROUP BY DAYOFWEEK(sale_date)
This returns the day and total sales for each day, which is great.
Thing is, there are multiple rows 'per sale' if there is more than one item per sale. So the total price is duplicated a number of times. So i can't just get the sum of total_price.
I need to get the sum of the total_price, ONLY for each UNIQUE sale_id.
eg
sale id | item | itemqty | item value | item total | total_price |
---1----|-drum-|-1-------|-60.00------|-60.0-------|-$230.00-----|
---1----|-uke--|-1-------|-170.00-----|-170.0------|-$230.00-----|
In MySQL, you can group by the sale_id. This will eliminate the duplicates:
SELECT DAYNAME(sale_date), SUM(total_price + total_tax)
FROM (select s.*
from threewor_vend.vendsales s
group by sale_id
) s
WHERE s.register_id = '07709f8e-8d90-11e0-8e09-4040f540b50a'
AND sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
GROUP BY DAYOFWEEK(sale_date);
As long as the other columns -- sale_date, total_price, total_tax, and register_id -- are the same on all the rows for a given sale_id, then this will work.
Since you're storing the total_price multiple times per sale, your results are being multiplied.
Here's one option using a subquery with DISTINCT assuming the four fields are all the same for each duplicated sale:
SELECT DAYNAME(sale_date), SUM(total_price + total_tax)
FROM (
SELECT DISTINCT sale_date, total_price, total_tax, register_id
FROM threewor_vend.vendsales
WHERE s.register_id = '07709f8e-8d90-11e0-8e09-4040f540b50a'
AND sale_date >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
) t
GROUP BY DAYOFWEEK(sale_date)