Mysql date_sub last 7 days result? - mysql

In mysql i am trying to get last 7 days data so i tried with following query
date_sub(curdate(), interval 7 day) <= visited_time from visitor table gives date from 5/10/2013 to 5/16/2013
but here the same query date_sub(curdate(), interval 7 day) <= date_entered from order table gives date from 5/9/2013 to 5/15/2013
Today is 5/16/2013 so it should give 5/9/2013 to 5/15/2013 as last 7 days result for visitor table also.
whats wrong on this ? kindly advice

I have followed the post and tried with Numbers table trick
DROP TABLE IF EXISTS `example`.`numbers`;
CREATE TABLE `example`.`numbers` (
`id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Populate the table using: INSERT INTO NUMBERS (id) VALUES (NULL) ...for as many values as you need.
Then finally tried with below query using left join with number table.
SELECT x.ts AS TIMESTAMP
FROM (
SELECT DATE_ADD( CURDATE( ) , INTERVAL n.id -7
DAY ) AS ts
FROM numbers n
WHERE DATE_ADD( CURDATE( ) , INTERVAL n.id -7
DAY ) <= CURDATE( )
)x
LEFT JOIN orders y ON ( y.`date_entered` ) = x.ts
GROUP BY DATE( x.ts )
ORDER BY DATE( x.ts ) DESC

Related

Create Temporary Table Populated with Dates and LEFT JOIN to `created_at` Column - MySQL

Original Query:
// Doesn't return dates with zero value
SELECT UNIX_TIMESTAMP( created_at ) AS DATE, COUNT( tweet_id ) AS count
FROM `tweets`
WHERE DATE( created_at ) > '2012-11-01'
AND DATE( created_at ) <= DATE( NOW( ) )
GROUP BY DATE
Modified Query:
// Attempting to return all dates with a value of zero if doesn't exist in `created_at` column
CREATE TEMPORARY TABLE DateSummary1 ( date timestamp ) SELECT DISTINCT(DATE(created_at)) as date FROM tweets;
CREATE TEMPORARY TABLE DateSummary2 ( date timestamp, number int ) SELECT DATE(created_at) as date, count(*) AS number FROM tweets WHERE DATE(created_at) > '2012-11-01' AND DATE(created_at) <= DATE(NOW()) GROUP BY DATE(created_at) ORDER BY created_at ASC;
SELECT ds1.date,ds2.number FROM DateSummary1 ds1 LEFT JOIN DateSummary2 ds2 on ds1.date=ds2.date;
Unfortunately, the latter result isn't providing me the dates with zero values like I had expected. What am I overlooking? I'm sure this is a logic error, but I'm not sure where my logic has faulted. I've gotten this far from reading copious threads on SO, Google, etc. but am not sure how to get over this final hurdle.
An example of the returned timestamps using jcho360's suggestion:
1387286811
1387286812
1387286813
1387286815
1387286820
Try this
SELECT UNIX_TIMESTAMP( created_at ) AS DATE, COUNT( tweet_id ) AS count
FROM `tweets`
WHERE (DATE( created_at ) > '2012-11-01'
AND DATE( created_at ) <= DATE( NOW( ) ) )
or DATE( created_at ) = date ('000-00-00') //I added this Line
GROUP BY DATE

Correct my MySQL query?

I have table like
CREATE TABLE `survey` (
`id` int(11) NOT NULL auto_increment,
`submitdate` datetime default NULL,
`answer` varchar(5) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=499 ;
now to get values like
c t Clicks
2012-10-29 2012-10-22 10
2012-11-04 2012-10-30 20
2012-11-11 2012-11-05 30
2012-11-19 2012-11-12 34
I am using this query
SELECT uq.timespan, COALESCE(tsq.TotalClicks, 0) as Clicks FROM (
SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -21
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -28
DAY ) ) l
union SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -15
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -20
DAY ) ) l
union SELECT DATE( DATE_ADD( NOW( ) , INTERVAL -8
DAY ) ) c, DATE( DATE_ADD( NOW( ) , INTERVAL -14
DAY ) ) l
union SELECT curdate() c,DATE( DATE_ADD( NOW( ) , INTERVAL -7
DAY ) ) l
)uq LEFT JOIN (
SELECT CASE
WHEN submitdate >= NOW() - INTERVAL 4 WEEK
AND submitdate < NOW() - INTERVAL 3 WEEK THEN c 'to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
AND submitdate < NOW() - INTERVAL 2 WEEK THEN c 'to' l
WHEN submitdate >= NOW() - INTERVAL 2 WEEK
AND submitdate < NOW() - INTERVAL 1 WEEK THEN c 'to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN c 'to' l
END Weeksubmitdate,
count(id) TotalClicks
FROM survey
WHERE submitdate >= NOW() - INTERVAL 4 WEEK
GROUP BY Weeksubmitdate
)tsq ON uq.timespan = tsq.Weeksubmitdate";
problem is with 16th line c to l.
I am getting the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''to' l
DAY ) )
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
' at line 16
CASE is supposed to evaluate to a scalar expression. That means its THEN clauses must evaluate to scalar expressions too. Now, what does this c 'to' l thing stand for? Is it a scalar expression? It doesn't seem one to me, however I may be unaware of some things in MySQL, so it's more important whether MySQL itself recognises that as a scalar expression. And apparently it doesn't.
There is another issue. You are trying to reference a derived table's columns inside another derived table. More specifically, you seem to be trying to reference the columns c and l of uq inside the tsq subselect, and that is illegal. If uq was a normal table, it would be fine, but since it is a virtual table, the query doesn't know about its existence at that point, i.e. at the time of parsing the tsq subquery.
Anyway, what you seem to be doing with your query could probably be rewritten more simply, like this, for instance:
SELECT
MIN(submitdate) AS startdate,
MAX(submitdate) AS enddate,
COUNT(*) AS clicks
FROM (
SELECT
CASE
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN 1
WHEN submitdate >= NOW() - INTERVAL 2 WEEK THEN 2
WHEN submitdate >= NOW() - INTERVAL 3 WEEK THEN 3
WHEN submitdate >= NOW() - INTERVAL 4 WEEK THEN 4
END AS weekid,
*
FROM survey
) s
GROUP BY
weekid
ORDER BY
startdate
;
The subquery assigns surrogate week IDs to every row of survey. The main query groups the results by those IDs and produces the counts as well as starting & ending dates for every group.

mysql get sum of hours / minutes / seconds

I have a table with: userid and timestamp each time a user opens a page a new field is inserted.
I am trying to get the total amount of hours / minutes / days / weeks that appear in a 1 month interval for multiple users.
I have tried a bunch of different queries but each have ended up terribly inefficient.
Ideally I'd like to end up with something like:
userid | minutes | hours | days | weeks
1 10080 168 7 1
2 1440 24 1 0
Hopefully someone can shed some light on how to do this.
Below is a query that I tried:
SELECT
w.time AS `week`,
d.time AS `day`,
h.time AS `hour`,
m.time AS `minutes`
FROM (
SELECT
SUM( t.time ) AS `time`
FROM (
SELECT
COUNT( DISTINCT WEEK( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY MONTH( `timestamp` )
) t
) w,
(
SELECT
SUM( t.time ) AS `time`
FROM (
SELECT
COUNT( DISTINCT DAY( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "52"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY MONTH( `timestamp` )
) t
) d,
(
SELECT
SUM( t.timestamp ) AS `time`
FROM (
SELECT
COUNT( DISTINCT HOUR( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY DAY( `timestamp` )
) t
) h,
(
SELECT
SUM( t.timestamp ) AS `time`
FROM (
SELECT
COUNT( DISTINCT MINUTE( `timestamp` ) ) AS `time`
FROM table
WHERE
userid = "1"
AND
`timestamp` > DATE_SUB( NOW( ) , INTERVAL 1 MONTH )
GROUP BY HOUR( `timestamp` )
) t
) m
It seems awfully excessive for this task, maybe someone has something better?
It's not clear to me what you want to "total".
If you want to determine whether a user had a "hit" (or whatever transaction it is you are storing in the table) at any given minute within the month), and then you want to count the number of "minute periods" within a month that a user had a hit:
SELECT t.userid
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d %H:%i')) AS minutes
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d %H' )) AS hours
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%Y-%m-%d' )) AS days
, COUNT(DISTINCT DATE_FORMAT(t.timestamp,'%X-%V' )) AS weeks
FROM mytable t
WHERE t.timestamp >= '2012-06-01'
AND t.timestamp < '2012=07-01'
GROUP BY t.userid
What this is doing is taking each timestamp, and putting it into a "bucket", by chopping off the seconds, chopping off the minutes, chopping off the time, etc.
Basically, we're taking a timestamp (e.g. '2012-07-25 23:15:30') and assigning it to
minute '2012-07-25 23:15'
hour '2012-07-25 23'
day '2012-07-25'
A timestamp of '2012-07-25 23:25:00' would get assigned to
minute '2012-07-25 23:25'
hour '2012-07-25 23'
day '2012-07-25'
Then we go through and count the number of distinct buckets we assigned a timestamp to. If that's all the hits for this user in the month, the query would return a 2 for minutes, and a 1 for all other period counts.
For a user with a single hit within the month, all the counts for that user will be a 1.
For a user that has all their "hits" within exactly the same minute, the query will again return a 1 for all the counts.
(For a user with no "hits" within a month, no row will be returned. (You'd need to join another row source to get a list of users, if you wanted to return zero counts.)
For a user with a "hit" every second within a single day, this query will return counts like that shown for userid 2 in your example.
This result set gives you a kind of an indication of a user's activity for a month... how many "minute periods" within a month the user was active.
The largest value that could be returned for "days" would be the number of days in the month. The largest possible value to be returned for "hours" would be 24 times the number of days in the month times. The largest possible value returned for "minutes" would be 1440 times the number of days in the month.
But again, it's not entirely clear to me what result set you want to return. But this seems like a much more reasonable result set than the one from the previously "selected" answer.
SELECT userid, SUM(MINUTE(timestamp)) AS minutes, SUM(MINUTE(timestamp))/60 AS hours, SUM(MINUTE(timestamp))/(60*24) AS days, SUM(MINUTE(timestamp))/(60*24*7) AS weeks
FROM Table
GROUP BY userid
If neccesary, use ROUND(SUM(MINUTE(timestamp)), 0) if you want integer numbers.

mysql select 3days records from today

SELECT * FROM `user`
WHERE name !='' AND `date_created` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 3 Day )
AND DATE_SUB( CURDATE( ) ,INTERVAL 0 Day )
ORDER BY `date` ASC
The above query brings record 3day before from todays date.
but i need 3day records from today,which means tomorrow , day after tomorrow etc.
date_created is mysql date format.
SELECT * FROM `user`
WHERE name !=''
AND `date_created` BETWEEN curdate() and curdate() + interval 3 day
ORDER BY `date`
I have created this will return previous 3 days record
select * from events where DATEOFEVENT IN (select date(curdate()-3 ))

MySQL Specialists: Delete all rows older than x days but not the latest one

first of all, this is the query which creates the "player history"
it can be executed as often as you want and it will only create new history rows for the players if there is no history row for yesterday or if the values changed since the latest history entry in the past.
INSERT INTO `player_history` (`player_id`, `date`, `races`, `maps`, `playtime`, `points`)
SELECT `p`.`id`, DATE_SUB(NOW(), INTERVAL 1 DAY), `p`.`races`, `p`.`maps`, `p`.`playtime`, `p`.`points`
FROM `player` `p`
WHERE `p`.`playtime` IS NOT NULL
AND `p`.`playtime` > 0
AND (
SELECT `player_id`
FROM `player_history`^
WHERE `player_id` = `p`.`id`
AND (
`date` = DATE_SUB(NOW(), INTERVAL 1 DAY)
OR (
`date` < DATE_SUB(NOW(), INTERVAL 1 DAY)
AND `races` = `p`.`races`
AND `points` = `p`.`points`
AND `maps` = `p`.`maps`
AND `playtime` = `p`.`playtime`
)
)
ORDER BY `date` DESC
LIMIT 1
) IS NULL;
now the problem is i also want to cleanup the history table using a single query. this already selects all history entries older than 10 days but the latest. but i cant just like do DELETE instead of SELECT *.
SELECT *
FROM `player_history` `ph`
WHERE `date` < DATE_SUB(NOW(), INTERVAL 10 DAY)
AND `date` != (SELECT `date`
FROM `player_history`
WHERE `player_id` = `ph`.`player_id`
ORDER BY `date` DESC
LIMIT 1);
so is tehre a way to do what i want using a single delete query?
Your query looks right in my eyes but you don't have the interval in the subquery.
I would do this:
DELETE FROM player_history
WHERE date < DATE_SUB(NOW(), INTERVAL 10 DAY)
AND date != (
SELECT MAX(date) FROM player_history
WHERE date < DATE_SUB(NOW(), INTERVAL 10 DAY)
)
What's the error message from mysql?
Probably you can't do this in a single query because the documentation states:
Currently, you cannot delete from a table and select from the same table in a subquery.
As a workaround you could select the ids of the rows that have to be deleted into a temporary table and then use a multi-table delete statement to delete the records from the original table.