I understand that it is not possible to perform calculations on aliases directly, but is there a way that I could find the sum of my alias(totaltime)? Thanks for the help in advance.
SELECT *,
TIMEDIFF( time2, time1 ) ,
TIMEDIFF( time4, time3 ) ,
ADDTIME( TIMEDIFF( time2, time1 ) ,
TIMEDIFF( time4, time3 ) ) ,
TIME_FORMAT( ADDTIME( TIMEDIFF( time2, time1 ) ,
TIMEDIFF( time4, time3 ) ) , '%H:%i' )
AS totaltime
SUM(SELECT(totaltime)) AS sumtotal
FROM
timer
WHERE
groupid='100'
AND
MONTH(`date`) = MONTH(NOW())
GROUP BY userid
Just add another layer of abstraction ;)
SELECT TIME_FORMAT(sec_to_time(sum(timecard.totaltime)),'%H:%i' ) FROM
(
SELECT *,
TIMEDIFF( out_time, in_time ) ,
TIMEDIFF( lunch_in, lunch_out ) ,
ADDTIME( TIMEDIFF( out_time, in_time ) , TIMEDIFF( lunch_in, lunch_out ) ) ,
ADDTIME( TIMEDIFF( out_time, in_time ) ,
TIMEDIFF( lunch_in, lunch_out ) )
AS totaltime
FROM
timecard
WHERE
comp_id='129'
AND
MONTH(`date`) = MONTH(NOW())
GROUP BY emp_id
) timecard
Link to fiddle: http://sqlfiddle.com/#!2/b33d8/16/0
Related
Below my query:
SELECT DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d %H:%i:%s' ) AS timing, temperature, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) >= '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) <= '2018-07-27'
ORDER BY timing ASC
LIMIT 0 , 30
It can be by the following query
SELECT DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d %H:%i:%s' ) AS timing, AVG( temperature ) AS avg, deviceid
FROM tempdata
WHERE deviceid =29
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) >= '2018-07-20'
AND DATE_FORMAT( FROM_UNIXTIME( `temp_timing` ) , '%Y-%m-%d' ) <= '2018-07-27'
GROUP BY DATE( timing ) , HOUR( timing )
ORDER BY `timing` ASC
LIMIT 0 , 30
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.)
SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL
(SELECT 'used' FROM b_names_2)) AS t
WHERE 'used' = 1
This code words very well..
But I want to sort the results with time intervals, so when I tried just to add this lines at the bottom, it doesnt work:
AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
The piece of code looked like this:
SELECT COUNT(*) AS cnt FROM ( (SELECT 'used' FROM b_names_1) UNION ALL
(SELECT 'used' FROM b_names_2)) AS t
WHERE 'used' = 1 AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' ) AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
it gives me the following error: #1054 - Unknown column 'registration_date_time' in 'where clause'
any idea how to correctly implement that date code to the SQL?
Those columns have to come FROM somewhere, add them to the inner queries:
SELECT COUNT(*) AS cnt
FROM (
(SELECT 'used', registration_date_time FROM b_names_1)
UNION ALL
(SELECT 'used', registration_date_time FROM b_names_2)
) AS t
WHERE 'used' = 1
AND registration_date_time >= STR_TO_DATE( '2013-01-28 00:00:00', '%Y-%m-%d %H:%i:%s' )
AND registration_date_time < STR_TO_DATE( '2013-02-02 00:00:00', '%Y-%m-%d %H:%i:%s' )
How to write this query in Zend
SELECT COUNT( 1 ) AS `NoOfQueries` , DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) AS `Dat` , COUNT( 1 ) + (
SELECT COUNT( 1 )
FROM tblsmsuser poocherb4
WHERE DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( `Dat` , INTERVAL 1
DAY ) ) AS cumulative_sum
FROM tblsmsuser poocher
GROUP BY `Dat`
What I have tried is
$this->select = new Zend_Db_Select($this->db);
$selctSub = $this->select->from("tblsmsuser",array("count(1)"))->where("DATE( CONVERT_TZ( FROM_UNIXTIME( tblsmsuser.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )");
echo $select_query_ans = $this->select->from(array("poocher" =>"tblsmsuser"),array("NoOfQueries"=>"count(1)","Dat"=>"DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) )","cumulative_sum"=>"count(1) + (".$selctSub.")"))->group("Dat");
Output
SELECT count(1) AS `NoOfQueries`, DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) ) AS `Dat`, count(1) + (SELECT count(1) AS `NoOfQueriesb4` FROM `tblsmsuser` AS ```poocherb4`` WHERE (DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )))`, count(1) AS `NoOfQueriesb4` FROM `tblsmsuser` AS `poocher` INNER JOIN `tblsmsuser` AS `` WHERE (DATE( CONVERT_TZ( FROM_UNIXTIME( poocherb4.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )) GROUP BY `Dat`
Here is the code which worked for me
$this->select = new Zend_Db_Select($this->db);
$selctSub = $this->select->from("tblsmsuser",array("count(1)"))->where("DATE( CONVERT_TZ( FROM_UNIXTIME( tblsmsuser.cdate, '%Y-%m-%d %T' ) , '-9:30', '+00:00' ) ) <= DATE_SUB( Dat , INTERVAL 1 DAY )");
$this->select = new Zend_Db_Select($this->db); //ADDED line
echo $select_query_ans = $this->select->from(array("poocher" =>"tblsmsuser"),array("NoOfQueries"=>"count(1)","Dat"=>"DATE( CONVERT_TZ( FROM_UNIXTIME( poocher.cdate,'%Y-%m-%d %T' ),'-9:30','+00:00' ) )","cumulative_sum"=>"count(1) + (".$selctSub.")"))->group("Dat");
is there a more efficent way to get rows fro ma table from the current week, last week, current month and current year to this
// current month
SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND MONTH( date_completed ) = MONTH( CURDATE( ) )
// current week
SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND WEEK( date_completed ) = WEEK( CURDATE( ) )
// last week
SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) ) AND WEEK( date_completed ) = WEEK( CURDATE( ) ) - 1
// current year
SELECT * FROM crm_tasks WHERE YEAR( date_completed ) = YEAR( CURDATE( ) )
You should be able to use a query similar to this
SELECT * FROM crm_tasks
WHERE date_completed >= #FirstDayOfYear AND date_completed < #FirstDayOfNextYear
Where #FirstDayOfYear and #FirstDayOfNextYear are filled in appropriately. If you have an index on date_created, this should work reasonably fast. This can be altered for any period you want to use.