This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate a running total in MySQL
I need to get the sum of counts which is grouped for each of the dates.Now I am running the following query and getting the out put as follows :
SELECT `timestamp` , COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
and I am getting
OUTPUT:
timestamp count(*)
-------------------------------------------------- -----------
2013-01-04 07:58:21 4
2013-01-05 09:28:56 38
2013-01-06 00:03:04 10
Now what I need is, I need to get the total sum of the counts grouped by date. That is for the second date it should be 42 and for third date it should be 52. How can I do this in a query?
Give it a try:
SELECT `timestamp` , #sum:= ifnull(#sum, 0 ) + COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
Try :
SELECT
timestamp t ,
(select count(*) from A where timestamp <= t)
FROM A
GROUP BY timestamp
ORDER BY timestamp
Can you try below SQL
SELECT DATE_FORMAT( ts_date , '%Y-%m-%d' ) as ts_dt_out, SUM(cnt)
FROM
(
SELECT `timestamp` ts_date , COUNT( * ) as cnt
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` , '%Y-%m-%d' )
)
as inner
WHERE ts_date >= '2013-01-04 07:12:12'
GROUP BY ts_dt_out
Note: Not tested let me know if it does not work
Here is a simple way
SELECT
`timestamp` ,
COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE(`timestamp`)
Related
SELECT COUNT( uid ) AS `Records` , DATE( FROM_UNIXTIME( 'since` ) ) AS `Date`
FROM `accounts` WHERE FROM_UNIXTIME(since) >= FROM_UNIXTIME($tstamp)
GROUP BY WEEK( FROM_UNIXTIME( `since` ) )
LIMIT 200
Was using this to try to get the New user signups daily from a specified date but its turning out to be incredibly inaccurate. Which means either my query is off or possibly there is some issue involving timezones?Below is a example result I got from a example data set I loaded in as well as a page worth of timestamps so you can see what the results should be.
It is suggested to use HAVING instead of WHERE with GROUP BY clause.
Also the backtick(`) operator is not used properly in this code.
So change this query:
SELECT COUNT( uid ) AS Records , DATE( FROM_UNIXTIME( 'since` ) ) AS `Date`
FROM `accounts` WHERE FROM_UNIXTIME(since) >= FROM_UNIXTIME($tstamp)
GROUP BY DATE( FROM_UNIXTIME( `since` ) )
LIMIT 200
to this one:
SELECT COUNT(`uid`) AS Records , DATE( FROM_UNIXTIME(`since`) ) AS Date
FROM accounts
GROUP BY DATE( FROM_UNIXTIME( `since` ) )
HAVING FROM_UNIXTIME(`since`) >= FROM_UNIXTIME($tstamp)
LIMIT 200
Alright I have tried alot and this looks just about right for me , but its def not:
SELECT COUNT( DISTINCT 'uid' ) AS `Records` , DATE( FROM_UNIXTIME( `epoch_timestamp` ) ) AS `Date`
FROM `log`
GROUP BY DATE( FROM_UNIXTIME( `epoch_timestamp` ) )
LIMIT 0 , 30
For w.e reason it returns a 1 next to each date. If I take out the distinct it appears to give a total records for that day count.
Seems like your sql is incorrect, try replacing the single quotation marks around 'uid' with `.
SELECT COUNT( DISTINCT `uid` ) AS `Records` , DATE( FROM_UNIXTIME( `epoch_timestamp` ) ) AS `Date`
FROM `log`
GROUP BY DATE( FROM_UNIXTIME( `epoch_timestamp` ) )
LIMIT 0 , 30
I have time range in database:
date temp
2014-05-09 20:40:01 19.6875
2014-05-09 20:50:01 19.375
.....................
2014-05-10 00:10:01 17.5
........................
2014-05-23 08:25:01 27.4375
And i want get all AVG temperature with week group by hour. Here sql query:
SELECT AVG( `temp` ) , `date` FROM `temperature` WHERE `date` BETWEEN '2014-05-16 11:06' AND '2014-05-23 11:06' GROUP BY HOUR( `date` )
But in result i have only value with range from 2014-05-16 23:06:02 to 2014-05-17 00:05:01
And not have error.
Help me find my mistake.
Sory for my bad English
This is because when you use GROUP BY and display a field that isn't being grouped, mysql can't show you all the values of that field. So it just shows one.
This query makes more sense:
SELECT AVG( `temp` ) , HOUR(`date`) FROM `temperature` WHERE `date` BETWEEN '2014-05-16 11:06' AND '2014-05-23 11:06' GROUP BY HOUR( `date` )
Or this one (after discussion)
SELECT AVG( temp ) ,
YEAR(date),
MONTH(date),
DAY(date),
HOUR(date)
FROM temperature
WHERE date BETWEEN '2014-05-16 11:06' AND '2014-05-23 11:06'
GROUP BY YEAR(date),MONTH(date),DAY(date),HOUR(date)
i have a table with id | type | publishedon
type may be 1,2,3 or 4 (int) value
i want to select posts for every day
now i'm using
SELECT FROM_UNIXTIME( `publishedon` , "%Y-%m-%d" ) AS `day` , count( id ) AS listings,
TYPE FROM posts
WHERE (
FROM_UNIXTIME( publishedon ) >= SUBDATE( NOW( ) , 30 )
)
GROUP BY `day`
the result
day listings
2013-09-02 17
2013-09-05 105
i want make listings filed more detailed like
day type_1 type_2 type_3 type_4
2013-09-02 10 4 6 3
2013-09-05 6 4 1 3
You simply need to put all your type values:
SELECT
FROM_UNIXTIME( `publishedon` , "%Y-%m-%d" ) AS `day`,
count(id) AS listings,
(SELECT COUNT(id) FROM `posts` WHERE `type`=1 AND FROM_UNIXTIME(`publishedon`, "%Y-%m-%d")=`day`) AS `type_1`,
(SELECT COUNT(id) FROM `posts` WHERE `type`=2 AND FROM_UNIXTIME(`publishedon`, "%Y-%m-%d")=`day`) AS `type_2`,
(SELECT COUNT(id) FROM `posts` WHERE `type`=3 AND FROM_UNIXTIME(`publishedon`, "%Y-%m-%d")=`day`) AS `type_3`,
(SELECT COUNT(id) FROM `posts` WHERE `type`=4 AND FROM_UNIXTIME(`publishedon`, "%Y-%m-%d")=`day`) AS `type_4`
FROM
`posts`
WHERE
FROM_UNIXTIME(`publishedon`) >= SUBDATE(NOW(), 30)
GROUP BY
`day`
but in fact, that will work slow since there are functions in conditions. If it is only a formatting matter, it's better to act like:
SELECT
FROM_UNIXTIME(`publishedon`, "%Y-%m-%d" ) AS `day`,
`type`,
count( id ) AS listings,
FROM
`posts`
WHERE
-- this should be better evaluated in application
-- since will not produce index using too:
FROM_UNIXTIME(`publishedon`) >= SUBDATE(NOW(), 30)
GROUP BY
`day`,
`type`
and then create desired formatting inside application.
I'm trying to query my reward database to work out how many points members of staff have allocated in total and this week.
The query that I'm using to work out the total points a teacher has allocated is as follows:
SELECT `Giver_ID` , SUM( `Points` ) AS TotalPoints
FROM `transactions`
GROUP BY `Giver_ID`
ORDER BY `Giver_ID` ASC
The query that I'm using to work out weekly allocations is very similar:
SELECT `Giver_ID` , SUM( `Points` ) AS WeeklyPoints
FROM `transactions`
WHERE ( `Datetime` >= '2012-09-24' AND `Datetime` <= '2012-09-30' )
GROUP BY `Giver_ID`
ORDER BY `Giver_ID` ASC
My question is this: is it possible to combine the queries to produce Giver_ID, TotalPoints and WeeklyPoints from a single query?
Thanks in advance,
Yes, it is possible -
SELECT
Giver_ID,
SUM(Points) AS TotalPoints,
SUM(IF(Datetime >= '2012-09-24' AND Datetime <= '2012-09-30', Points, NULL)) AS WeeklyPoints
FROM transactions
GROUP BY Giver_ID
Try this:
SELECT a.`Giver_ID` ,
MAX(b.`TotalPoints`) as `TotalPoints`,
MAX(c.`WeeklyPoints`) as `WeeklyPoints`
FROM `transactions` as a
LEFT JOIN (SELECT `Giver_ID`, SUM(`Points`) AS TotalPoints FROM `transactions` GROUP BY `Giver_ID`) as b ON a.`Giver_ID`=b.`Giver_ID`
LEFT JOIN (SELECT `Giver_ID`, SUM(`Points`) AS WeeklyPoints FROM `transactions` WHERE ( `Datetime` >= '2012-09-24 00:00:00' AND `Datetime` <= '2012-09-30' ) GROUP BY `Giver_ID`) as c ON a.`Giver_ID`=c.`Giver_ID`
GROUP BY a.`Giver_ID`
ORDER BY a.`Giver_ID` ASC
SELECT `Giver_ID` , SUM( `Points`) AS WeeklyPoints,(SELECT SUM(`Points`)
FROM `transactions` where `Giver_ID`=t.`Giver_ID` GROUP BY `Giver_ID`) AS TotalPoints
FROM `transactions` t
WHERE ( `Datetime` >= '2012-09-24' AND `Datetime` <= '2012-09-30' )
GROUP BY `Giver_ID`
ORDER BY `Giver_ID` ASC