MySQL Distinct Active Users for the last month query - mysql

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

Related

Inaccurate New User Query

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

MySQL query does not run in SQLite

SELECT MAX( PRC_MIN_LENGTH ) PRC_MIN_LENGTH, MIN( PRC_MAX_LENGTH ) PRC_MAX_LENGTH, MAX( PRC_MIN_WIDTH ) PRC_MIN_WIDTH, MIN( PRC_MAX_WIDTH ) PRC_MAX_WIDTH
FROM (
SELECT PRDT_PRICE_CODE, MIN( PRC_MIN_LENGTH ) PRC_MIN_LENGTH, MAX( PRC_MAX_LENGTH ) PRC_MAX_LENGTH, MIN( PRC_MIN_WIDTH ) PRC_MIN_WIDTH, MAX( PRC_MAX_WIDTH ) PRC_MAX_WIDTH
FROM PRODUCT_PRICE_INFO
WHERE PRDT_PRICE_CODE
IN (
SELECT PRDT_PRICE_CODE
FROM PRODUCT
WHERE PRODUCT_ID =1
UNION SELECT PRDT_PRICE_CODE
FROM PRODUCT_OPTION
WHERE PROD_OPT_ID
IN (
'1', '101', '201', '303', '401'
)
)
AND CURDATE( )
BETWEEN DATE_SUB( CURDATE( ) , INTERVAL 1
DAY )
AND DATE_ADD( CURDATE( ) , INTERVAL 1
DAY )
GROUP BY PRDT_PRICE_CODE
)PRC_RANGE
This query is running in MySQL database but not in SQLite.
Where is the mistake and how can I fix this?
SQLite uses different date functions.
You would have to write the date comparison like this:
...
AND date('now') BETWEEN date('now', '-1 days')
AND date('now', '+1 days')
...
(This is a faithful translation, and will make the query run; but it's doubtful that this query does what you want in either MySQL or SQLite.)

count of sum with group by [duplicate]

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`)

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'