I am trying to put together a query that groups records by date along with a total for that particular date (there can be multiple entries in a day) but I also need a running total that I intended on using a MySQL variable for. My issue is that the cumulative total column seems to contain the SUM() for just that date.
So this works fine for the daily totals
SELECT
YEAR(log_at) as year,
MONTH(log_at) as month,
DAY(log_at) as day,
SUM(ev) as dailyTotal,
count(*) as count
FROM tracks
WHERE user_id = 1
GROUP BY year, month, day
ORDER BY year, month, day ASC
Add what I thought was going to be a fairly simple variable in there to keep track of the running total
SET #cumulative := 0;
SELECT
YEAR(log_at) as year,
MONTH(log_at) as month,
DAY(log_at) as day,
SUM(ev) as dailyTotal,
#cumulative := #cumulative + sum(ev) AS cumulative,
count(*) as count
FROM tracks
WHERE user_id = 1
GROUP BY year, month, day
ORDER BY year, month, day ASC
And the cumulative variable just contains the total for that day. But if I change it to increment by 1 instead of the SUM() it seems to work correctly
Any advice for achieving the desired behaviour is greatly appreciated!
You have to first create the whole selection and only then do your cumulative-stuff because you can't append the sum of a column of your GROUP BY at the same time (I don't really know why dough)
SELECT
`year`,
`month`,
`day`,
`dailyTotal`,
#cum := #cum + `dailyTotal` as `cumulative`,
`count`
FROM
(
SELECT
YEAR(log_at) as year,
MONTH(log_at) as month,
DAY(log_at) as day,
SUM(ev) as dailyTotal,
count(*) as count
FROM tracks
WHERE user_id = 1
GROUP BY year, month, day
ORDER BY year, month, day ASC
) a
JOIN (SELECT #cum := 0) b
I tested it with a bit less intensive tables... maybe I got this one wrong here. But I hope you get the theory.
Related
I want to get a statistic for every month of the years i have in DB
SELECT monthname(created_at) AS month, YEAR(created_at) AS year, count(*) AS number
FROM tableName
WHERE type_of_user = "someType"
GROUP BY year, month(created_at)
ORDER BY created_at DESC
Now it gives me only month that I have, but I need to get statistics for every month, even if I don't have any stored data for that month
Create a calendar table. This will need one entry per month, for every year that you intend to use.
Then select from the calendar table, and join in the values that you get from your current query. Use COALESCE() to put a zero-value where the entry is NULL (e.g. when there are no records in the tableName for that month and year).
SELECT MONTHNAME(date) as month,
YEAR(date) as year,
COALESCE(number, 0) as number
FROM calendar AS C
LEFT JOIN (
SELECT created_at, COUNT(*) as number
FROM tableName AS T
WHERE T.type_of_user = 'someType'
GROUP BY YEAR(created_at), MONTH(created_at)
) AS T
ON MONTH(T.created_at) = MONTH(C.date) AND YEAR(T.created_at) = YEAR(C.date)
GROUP BY month, YEAR(created_at)
ORDER BY MONTH(date), YEAR(date)
SQL fiddle at http://sqlfiddle.com/#!9/e0a4dc/
SELECT month(created_at) as month
FROM tableName
RIGHT JOIN (select row_number() over (order by 1) as i
from someTableWithMoreThan12Records limit 12) x
ON x.i=month(created_at)
ORDER BY I;
JOINing with a table that has all the records will give you every month.
I have a list of data contians (yyyy-mm-dd, hh:min:ss) from this data i need to sort out how many times 'logins' was punched for any given hour of the day. and also how many times logins' was punched for each day of the week .
I tried some code but am not confident the code i have right. Also should i include COUNT(*) in the statement
This code for hourly logins :
SELECT date_time, HOUR(date_time) FROM time_logs ORDER BY DAY(date_time);
Code for day for week:
SELECT date_time, DAY(date_time) FROM time_logs ORDER BY DAY(date_time);
Is this is right. If not could you give me hint
To count how many logins per hour, you just need to add COUNT(*) in your SELECT and GROUP BY HOUR(date_time).
SELECT date_time, HOUR(date_time),COUNT(*) 'Total Logins'
FROM time_logs GROUP BY HOUR(date_time) ORDER BY DAY(date_time);
Similarly on your day count you need to add the same thing. Only difference is your grouping is now by day.
SELECT date_time, DAY(date_time), COUNT(*) 'Total Logins'
FROM time_logs GROUP BY DAY(date_time) ORDER BY DAY(date_time);
You can cast your datetime to both hour and date and then group by to get your aggregations.
https://rextester.com/BQSV80644
For hour of the day:
SELECT HOUR(date_time), COUNT(*)
FROM time_logs
GROUP BY HOUR(date_time);
For day of the week:
SELECT WEEKDAY(date_time), COUNT(*)
FROM time_logs
GROUP BY WEEKDAY(date_time)
ORDER BY MIN(date_time)
Basically I have a table like this:
Table Time:
ID.......Date
1......08/26/2016
1......08/26/2016
2......05/29/2016
3......06/22/2016
4......08/26/2015
5......05/23/2015
5......05/23/2015
6......08/26/2014
7......04/26/2014
8......08/26/2013
9......03/26/2013
The query should return like this
Year........CountNum
2016........4
2015........3
To find out which year does its value tend to increase in. I notice that I want to display the years that have more values (number of row in this case) than the previous year.
What I've done so far
SELECT Year, count(*) as CountNum
FROM Time
GROUP BY Year
ORDER BY CountNum DESC;
I don't know how to get the year from date format. I tried year(Date) function, but I got Null data.
Please help!
It should works fine.
select year(date), count(*) as countNum
from time
group by year(date)
order by countNum
Join the grouped data to itself with 1 year offset:
select
a.*
from
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) a
left join
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) b
on a._year = b._year-1
where a._n > b._n
order by 1
I have a table of sold items, with specified name, day, hour and amount of items sold.
What I need to do is for every day find the hour in which the greatest number of items (of any type) was sold and return the two-columned table with day and amount of items.
What I managed to do is to compute the sum of items per hour, but how to pick the hour with maximum amount of items sold and show it together with the day?
here is my lousy sqlfiddle attempt: http://sqlfiddle.com/#!9/93b51/17/0
select day, hour, sum(amount) as suma
from sold_items
group by day, hour
You need to join your query with juergen d's query that gets the maximum hourly amount each day.
SELECT a.day, a.hour, a.suma
FROM (
select day, hour, sum(amount) as suma
from sold_items
group by day, hour) AS a
JOIN (
select day, max(suma) AS maxsuma
from (
select day, hour, sum(amount) as suma
from sold_items
group by day, hour) AS tmp
group by day) AS b
ON a.day = b.day AND a.suma = b.maxsuma
DEMO
This follows the same pattern as SQL Select only rows with Max Value on a Column except that in this case, you're doing it with a subquery that calculates an aggregate, not the data coming directly from the table.
select day, max(suma)
from
(
select day, hour, sum(amount) as suma
from sold_items
group by day, hour
) tmp
group by day
SQLFiddle
I'm trying to group posts from same day, the problem is that 2/20 gets grouped with 3/20 (20 = 20)
How can this be fixed?
This is my current code:
select day(Date), count(*) from Posts WHERE shopID != '' group by shopID, day(Date)
You need to group by every piece that might be different. So add MONTH(Date) and even YEAR(Date) depending on the scope of your query.
select DAY(Date), count(*) from Posts WHERE shopID != '' group by shopID, YEAR(Date), MONTH(Date), DAY(Date)
You could also group this way: UNIX_TIMESTAMP(date(date)), instead of grouping by year, month and day separately
select date(date), count(*) from Posts
WHERE shopID != ''
group by shopID, UNIX_TIMESTAMP(date(date))
Note you'll have to also take the other date data in the select statement to be able to recognize which month/year the day belongs to. If you don't you'll get a lot of day numbers and counts, but the day numbers will be repeated for each month/year.
That's why I used date(date), count(*).