Retrieving Result in subselect by timestamp matching fields per row - mysql

with this query i aim to retrieve data from another table in a subquery where arguments compare unixtime stamp. But the result in my third columne ('valid') remains empty?
SELECT COUNT( valid.call_id )FROM calls AS valid WHERE SECOND((
SELECT conf.cca_setvalidtime FROM user_conf AS conf
WHERE MONTH(FROM_UNIXTIME(conf.activ_date)) = MONTH(FROM_UNIXTIME(valid.last_call ))
)) < SECOND( valid.call_duration )
) AS 'valid'
FROM calls
GROUP BY EXTRACT(
YEAR_MONTH FROM last_call )

Here is the Solution for anyone who can need something alike ;-)
SELECT DATE_FORMAT( last_call, '%M' ) AS 'month', COUNT( call_id ) AS 'total', (
SELECT COUNT( valid.call_id )
FROM calls AS valid
WHERE TIME((
SELECT conf.cca_setvalidtime FROM user_conf AS conf
WHERE EXTRACT(YEAR_MONTH FROM conf.activ_date ) = EXTRACT(YEAR_MONTH FROM valid.last_call ) AND conf.for_role=1
)) < TIME( valid.call_duration )
AND EXTRACT(
YEAR_MONTH FROM valid.last_call ) = EXTRACT(
YEAR_MONTH FROM calls.last_call )
) AS 'valid'
FROM calls
GROUP BY EXTRACT(
YEAR_MONTH FROM last_call )

Related

Why am i getting this error message ( #1111 - Invalid use of group function )?

I am trying to retrieve the TIMEDIFF from the last_call field of every row, where TIMEDIFF between current row and next row is greather than 10 min!
Can someone please help? Meybe there is a better way of doing this?
My goal is to get the total of all timediff between severall database entrys but only if they are greather than 10 min.
SELECT DATE_FORMAT( last_call, '%d' ) AS 'day',
(
SELECT TIME_TO_SEC(TIMEDIFF(MAX(cl1.last_call), MIN(cl1.last_call)))
FROM calls AS cl1
WHERE TIME_TO_SEC(TIMEDIFF(MAX(cl1.last_call), MIN(cl1.last_call))) > 600
AND cl1.calling_agent=9
AND EXTRACT(DAY FROM cl1.last_call ) = EXTRACT(DAY FROM calls.last_call )
) AS 'brake'
FROM calls
WHERE calling_agent =9
AND last_call > DATE_SUB( now( ) , INTERVAL 12 MONTH )
GROUP BY EXTRACT( DAY FROM last_call )

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

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'