I have a datetime field in my db. Now i want to select upcoming dates from NOW() based on the dates. If two datetimes is the same I want to select them. And if it's only one I want to select that date. Limit is that I never want to select more than two rows.
So if NOW() is 2021-06-18 12:00:00 row number 1 should be selected.
If NOW() is 2021-06-20 18:15:00 row number 3 and 4 should be selected.
1 2021-06-19 18:00:00
2 2021-06-20 18:00:00
3 2021-06-21 15:00:00
4 2021-06-21 15:00:00
5 2021-06-21 18:00:00
6 2021-06-21 18:00:00
I've tried
SELECT gamedate
FROM games
WHERE gamedate > NOW()
ORDER BY gamedate LIMIT 0 , 1
but that doesn't make any sense or what I want to do.
Use a subquery to get the next gamedate. Then use that in the main query to select at most two rows with that date.
SELECT id, gamedate
FROM games AS g
WHERE gamemedate = (SELECT MIN(gamedate) FROM games WHERE gamedate > NOW())
ORDER BY id
LIMIT 2
Related
I have a table where records will be getting inserted every 4 hours on a daily basis. If the record was not inserted for continuous 4 hours, I need to insert a log into another table. Below is the table schema.
Id DocPathid CreatedAt
1 1 2021-04-02 00:00:00
2 1 2021-04-02 04:00:00
3 1 2021-04-02 09:00:00
4 1 2021-04-02 12:00:00
5 1 2021-04-02 16:00:00
6 1 2021-04-02 20:00:00
7 1 2021-04-02 24:00:00
In the above case, there was no records inserted within a interval of 4hours (i.e. between 2021-04-02 04:00:00 & 2021-04-02 09:00:00). The query should return no. of failure count (in this case it is failed for 1 time).
Is there a way to achieve this in MySQL?
You can do something like this.
select count(1)
from (
select id, CreatedAt , timestampdiff(hour, CreatedAt
, lead(CreatedAt,1) over (partition by DocPathid order by CreatedAt) ) as hour
from Table1
) t
where hour >4
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=9b0c631145422dbccd2ea23f0a7d2011
halo i want to count the data and group by month (month start date) then show in date time, any idea ?
table_a
timestamp
2020-11-28 04:00:00
2020-11-28 05:00:00
2020-12-29 01:00:00
2020-12-29 02:00:00
2020-12-29 03:00:00
expected result:
timestamp count
2020-11-01 00:00:00 2
2020-12-01 00:00:00 3
my query is:
SELECT STR_TO_DATE(DATE_FORMAT(timestamp, '%Y-%m-dd HH'), '%Y-%m-dd HH'), count(*) as count
from table_a
but my results was:
timestamp count
2020-11-00 2
2020-12-00 3
You can use:
select str_to_date(concat_ws('-', year(timestamp), month(timestamp), 1)) as yyyymm,
count(*)
from table_a
group by yyyymm;
i fixed the question with add group by month(timestamp) , i m ignored the minuted, the important things is group by month , here are my full query :
SELECT timestamp, count(*) as count
from table_a
group by month(timestamp)
then the output show :
timestamp count
2020-11-01 04:00:00 2
2020-12-01 01:00:00 3
I'm trying to get a query that will show number of visits per day for the last 7 days. Query that I come up with works but it has limitation I do not know how to get rid of.
Imagine, it is August 4th, 2019. Our table visits keeps timestamps of users visits to a website:
ID | timestamp
1 | 2019-08-03
2 | 2019-08-03
3 | 2019-08-02
4 | 2019-07-31
5 | 2019-07-31
6 | 2019-07-31
7 | 2019-07-31
8 | 2019-07-30
9 | 2019-07-30
10 | 2019-07-28
Objective: get number of visits to a website per day for the last 7 days. So the result should be something like:
DATE | NumberOfVisitis
2018-08-04 | 0
2018-08-03 | 2
2018-08-02 | 1
2018-08-01 | 0
2018-07-31 | 4
2018-07-30 | 1
2018-07-29 | 0
My query includes only dates registered in DB (it excludes days with no visits). This makes sense as query is data dependent, instead of calendar.
SELECT DATE_FORMAT(`timestamp`, "%Y%m/%d") AS Date, COUNT(`id`) AS
NumberOfVisitis FROM `visits` WHERE `timestamp` >= DATE_ADD(NOW(),
INTERVAL -7 DAY) GROUP BY DAY(`timestamp`) ORDER BY `timestamp` DESC
Can you please let me know how can I modify my query to include days with no visits in the query result?
MySQL lacks anything like Postgres's generate_series so we have to fake it.
Simplest thing to do is to make a table with a bunch of numbers in it. This will be useful for generating lots of things.
create table numbers ( number serial );
insert into numbers () values (), (), (), (), (), (), ();
From that we can generate a list of the last 7 days.
select date_sub(date(now()), interval number-1 day) as date
from numbers
order by number
limit 7
Then using that as a CTE (or a subquery) we left join it with visits. A left join means all dates will be present.
with dates as (
select date_sub(date(now()), interval number-1 day) as date
from numbers
order by number
limit 7
)
select date, coalesce(sum(id), 0)
from dates
left join visits on date = timestamp
group by date
order by date
I need to find the Daily total count of Active Users based on the Start Date and End Date.
REGISTRATION TABLE
id registration_no start_date end_date
1 1000 2014/12/01 2014/12/03
2 1001 2014/12/01 2014/12/03
3 1002 2014/12/02 2014/12/04
4 1003 2014/12/02 2014/12/04
5 1004 2014/12/02 2014/12/04
6 1005 2014/12/03 2014/12/05
7 1006 2014/12/05 2014/12/06
8 1007 2014/12/05 2014/12/09
9 1008 2014/12/06 2014/12/10
10 1009 2014/12/07 2014/12/11
The result should be in the following format.
Date Active Users
2014-12-01 2
2014-12-02 5
2014-12-03 6
2014-12-04 4
2014-12-05 3
2014-12-06 3
2014-12-07 3
2014-12-08 3
2014-12-09 3
2014-12-10 2
2014-12-11 1
2014-12-12 0
I know the following query is not working.
SELECT start_date, count(*) FROM registration
WHERE start_date >= '2014/12/01' AND end_date <='2014/12/12'
GROUP BY start_date
Which is not the desired output :
2014-12-01 2
2014-12-02 3
2014-12-03 1
2014-12-05 2
2014-12-06 1
2014-12-07 1
Any help would be much appreciated.
You need to create a "calendar" with all the days you need and then use a query like:
SELECT calDay as `Date`, count(id) as `Active Users`
FROM (SELECT cast('2014-12-01' + interval `day` day as date) calDay
FROM days31
WHERE cast('2014-12-01' + interval `day` day as date) < '2014-12-12') calendar
LEFT JOIN registration on (calDay between start_date and end_date)
GROUP BY calDay
ORDER BY calDay;
You can see it working in this fiddle, where days31 is just a view with integers 0-30. This allows the query to work in any calendar up to a period of 31 days. You can add more days to the view or generate them on the fly using cross joins. See http://www.artfulsoftware.com/infotree/qrytip.php?id=95
Try it.... please note on where condition FOR 2014-12-02, as per comment
SELECT DATE_FORMAT(start_date,'%Y-%m-%d')as Date, count(*) as ActiveUser FROM registration
WHERE (start_date >= '2014/12/02' AND end_date <='2014/12/02')
GROUP BY start_date
I have Table with 3 Columns, Column1 with Date and Column2 stores Points which is nothing but Some random Number between 1 to 9 and column 3 which have some unique email address in every cell.
Now I want to add the points grouped by date for last 5 days.
That is if I have 3 rows for day one, 1 rows for day two, 3 rows for day 3 and 2 rows for day 4 & 5 I should get the sum of points of these 11 rows grouped by date as 5 rows for five days.
Input
Date Points
Email
2012-07-01 5 a#sample.com
2012-07-01 6 b#sample.com
2012-07-01 2 c#sample.com
2012-07-02 5 d#sample.com
2012-07-03 8 e#sample.com
2012-07-03 7
f#sample.com
2012-07-03 1
y#sample.com
2012-07-04 3 x#sample.com
2012-07-04 2 f#sample.com
2012-07-05 3 g#sample.com
2012-07-05 9 b#sample.com
Output
Date Points
2012-07-01 13
2012-07-02 5
2012-07-03 16
2012-07-04 5
2012-07-05 12
Please suggest me a MySQL query for the above.
select `Date`,sum(`Points`) from my_table group by `Date`;
select `Date`,sum(`Points`) from my_table group by `Date` Limit 5;
select Date,sum(Points) from my_table group by Date;
select [Date],SUM(Points)
from myTable1
group by [Date]
You can do:
SELECT date, SUM(points) AS points
FROM tbl
WHERE date > CURDATE() - INTERVAL 5 DAY
GROUP BY date