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
Related
I have to sum the time in a table. In my table there is duration field and the type is "time" .
I have tried this query it returns only seconds. but I need hours and minutes and seconds.
in my table there is two rows one row has 00:20:10 and another is 00:15:05
so I need to display as 00:35:15
SELECT SEC_TO_TIME( SUM( MINUTE( duration ) ) ) AS totaltime
FROM users
please try this one:
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `duration` ) ) ) AS totaltime
Convert it to seconds and then back to time
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( duration ) ) ) AS totaltime
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
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 )
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.)