Inaccurate New User Query - mysql

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

Related

MySQL Distinct Active Users for the last month query

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

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

MySQL Query Deletion

I want to delete records from tbl_Billing and I want to use LIMIT while performing this function. Please suggest if it's possible using INNER JOIN or LEFT JOIN. The query is below
DELETE
FROM tbl_Billing
WHERE `aCreated` > '2011-01-09'
AND `aStatus` ='2'
AND `aUserUnique`
IN (
SELECT aUserUnique
FROM tbl_Users AS user
WHERE user.`aBillingRenew` < date_sub( now( ) , INTERVAL 60 DAY )
AND user.aActive =1
)
I got the answer. We have to use EXISTS instead of the IN operator:
DELETE
FROM tbl_Billing
WHERE `aCreated` > '2011-01-09'
AND `aStatus` ='2'
AND EXISTS
(
SELECT aUserUnique
FROM tbl_Users AS users
WHERE users.`aBillingRenew` < date_sub( now( ) , INTERVAL 60 DAY )
AND users.aActive ='1'
)
limit 10

Joining two mysql queries containing time functions

I need to join the result of the next two queries:
SELECT EXTRACT( HOUR
FROM (
TIMEDIFF( NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580' ) )
)
) AS HOURS
SELECT EXTRACT( MINUTE
FROM (
TIMEDIFF( NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580' ) )
)
) AS MINS
I know I have to use join them somehow but this Extract functions are giving me lots of problems. I have the next query that still works
SELECT
*
FROM
(SELECT
MIN(date_cmd) AS HOURS
FROM
arc_commande_temp
WHERE
id_cmd = '6580') AS HOURS,
(SELECT
MIN(date_cmd) AS MINS
FROM
arc_commande_temp
WHERE
id_cmd = '6580') AS MINS
it's not what I need but I'm trying to modify it to get what I want - but when I try to change something else in order to build the query I need... I get an error :-(
Thanks in advance and regards
SELECT
EXTRACT(HOUR FROM (TIMEDIFF(NOW( ), (
SELECT MIN(date_cmd)
FROM arc_commande_temp
WHERE id_cmd = '6580'
)))) AS HOURS
,
EXTRACT(MINUTE FROM (TIMEDIFF(NOW( ) , (
SELECT MIN( date_cmd )
FROM arc_commande_temp
WHERE id_cmd = '6580'
)))) AS MINS

get all data and divide one column into 3 other columns

SELECT date( `captureTime` ) dateDay, DATE_FORMAT( `captureTime` , '%H' ) dateHour, DATE_FORMAT( `captureTime` , '%i' ) dateQuarter, max( channel1_Data ) , max( channel2_Data ) , max( channel3_Data ) , min( channel1_Data ) , min( channel2_Data ) , min( channel3_Data )
FROM `sensordata`
WHERE `Sensor_sensorSerialNo` =1
AND `captureTime` >= '2011-10-16 22:15:11'
AND `captureTime` <= '2011-10-17 23:59:59'
I want to get all data from 2011-10-16 22:15:11 to 2011-10-17 23:59:59 after running the select query. And the 'captureTime' column also need be divided into three columns(i.e. 'dateDay', 'dateHour' and 'dateQuater'). However, only get one row result after running the above query. I can't see anything wrong there.
Can anyone point out the errors? Thanks in advance!
min, max functions are aggregate functions and are usually used with Group By clause. However If you are not using Group By clause then Min or Max value will be returned after performing calculation on complete table data. So, only one row should be returned, that would be minimum or maximum. If you want other columns to be selected, then those columns must be present in Group By clause.
MySql allowed this query to execute, but other dbms such as SQLServer wont even allow this query to be executed.
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Try using like:
SELECT date( `captureTime` ) as "dateDay", DATE_FORMAT( `captureTime` , '%H' ) as "dateHour", DATE_FORMAT( `captureTime` , '%i' ) as "dateQuarter", max( channel1_Data ) , max( channel2_Data ) , max( channel3_Data ) , min( channel1_Data ) , min( channel2_Data ) , min( channel3_Data ) FROM `sensordata` WHERE `Sensor_sensorSerialNo` =1 AND `captureTime` >= '2011-10-16 22:15:11' AND `captureTime` <= '2011-10-17 23:59:59'