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
Suggest me an MySQL Query for output like the above
try
SELECT `date`, SUM(`Points`) AS sumPoint FROM table GROUP BY `date`
Note : dont forget to wrap date column with ` as DATE is reserve keyword of mySQL
SELECT date, SUM(Points) AS sumPoint
FROM table
GROUP BY DATE(date)
Since date column might be datetime or date type in table
Related
I have table , with date format in YYYY-MM-DD.
Acc_No
On_board_date
Arrival_date
001
2022-02-01
2022-02-04
002
2022-03-10
2022-03-07
003
2022-03-12
2022-03-25
What I want is Acc_No's whose arrival date is +/- 5 days from on_board_date
which will result in like these
Acc_No
On_board_date
Arrival_date
001
2022-02-01
2022-02-04
002
2022-03-10
2022-03-07
This is what I have tried
select * from table
where on_board_date <= dateadd(arrival_date,5)
or on_board_date <= datesub(arrival_date,5)
Not getting desired result with this
You can try this :
select * from table
where on_board_date <= arrival_date + INTERVAL '5' day
I tried the one Akina suggested
WHERE DATEDIFF(On_board_date, Arrival_date) BETWEEN -5 AND 5
This worked.
I have a table:
Name Registered Date
Amit 2017-01-01
Akshay 2017-01-03
Ankith 2017-01-05
Amit 2017-01-12
Amit 2017-01-13
Amit 2017-02-01
Amit 2017-02-01
I want to write a query which will display the registration weekly report:
Say date between 2017-01-01 to 2017-03-01
Week Count
2017-01-01 3
2017-01-08 2
2017-01-15 0
2017-01-22 0
2017-01-29 2
2017-02-05 0
2017-02-12 0
2017-02-19 0
2017-02-26 0
Here Count is the number of people who registered that week. 3 people registered in between 2017-01-01 to 2017-01-07.
So which query i have to use for this result?
Thanks
If you can use the WEEK function and display the week number instead of a date, then:
select dummy.n, count(table.RegiseredDate)
from (SELECT 1 as n UNION SELECT 2 as n UNION SELECT 3 as n ... UNION SELECT 53 as n) dummy
left outer join table on dummy.n=WEEK(table.Registered Date)
where start_date>= x and end_date<= y
group by WEEK(Registered Date)
There's a DATETIME column called time. How could I select all rows that fall within the last existing 12 months (NOT within the last year from today)? Not every month might have a row, and months may have more than one row.
For example, out of this table (ORDER BY time DESC), rows with ids 2 to 17 would be selected.
id time
-- ----
17 2015-04-01
16 2015-04-01
15 2015-03-01
14 2015-02-01
13 2015-01-01
12 2014-12-01
11 2014-11-01
10 2014-10-01
9 2013-12-01
8 2013-11-01
7 2013-10-01
6 2013-09-01
5 2013-09-01
4 2013-09-01
3 2013-09-01
2 2013-08-01
1 2013-07-01
Another way to put this:
Take the table above and group by month/year, so we get:
2015-04
2015-03
2015-02
2015-01
2014-12
2014-11
2014-10
2013-12
2013-11
2013-10
2013-09
2013-08
2013-07
Now take the 12 most recent months from this list, which is everything except 2013-07.
2015-04
2015-03
2015-02
2015-01
2014-12
2014-11
2014-10
2013-12
2013-11
2013-10
2013-09
2013-08
And select everything from those months.
I guess I could do this with multiple queries or subqueries but is there another way to do this?
If your time field is only month-precision, you could do it with a pretty simple subselect:
SELECT * FROM Table t1
WHERE time IN (
SELECT DISTINCT time FROM Table t2 ORDER BY time DESC LIMIT 12
)
If your timestamps are full-precision, you could do the same thing, but you'd need to do some date manipulation to round the dates to the month for comparison.
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