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' )
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
I'm trying to get a count of how many times a user has triggered the following query. And I've concluded that a Sub Query is required.
The below (admittedly indelicate) query works, as far as it goes, without the Sub Query. And the Sub Query works as a standalone query. But after three days of trying, I cannot get the two to work combined. I don't know if I have a glaring syntax error, or whether I'm getting it all wrong in principle. I need help!
SELECT id, status, FirstName, LastName, Track, KeyChange, Version,
DATE_FORMAT(CONVERT_TZ(Created,'+00:00','+1:00'), '%l:%i %p') AS Created_formatted,
TIME_FORMAT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, pinknoise.Created, CURRENT_TIMESTAMP() - INTERVAL '0' HOUR)),'%Hh %im') AS elapsed,
(SELECT `FirstName`, Count(*) AS 'CountRequests' FROM `pinknoise` GROUP by `FirstName`)
FROM pinknoise
WHERE status = 'incoming'
ORDER BY Created DESC
I don't really understand what your query should achieve, but well formatted it looks like:
SELECT
id,
status,
FirstName,
LastName,
Track,
KeyChange,
Version,
DATE_FORMAT(
CONVERT_TZ(
Created,
'+00:00',
'+1:00'
),
'%l:%i %p'
) AS Created_formatted,
TIME_FORMAT(
SEC_TO_TIME(
TIMESTAMPDIFF(
SECOND,
pinknoise.Created,
CURRENT_TIMESTAMP() - INTERVAL '0' HOUR
)
),
'%Hh %im'
) AS elapsed
(
SELECT
`FirstName`,
Count(*) AS 'CountRequests'
FROM
`pinknoise`
GROUP by
`FirstName`
)
FROM
pinknoise
WHERE
status = 'incoming'
ORDER BY
Created DESC
What I imagine: you want the number of total entries for this particular firstname in the same table. The dirty way would be:
SELECT
id,
status,
FirstName,
LastName,
Track,
KeyChange,
Version,
DATE_FORMAT(
CONVERT_TZ(
Created,
'+00:00',
'+1:00'
),
'%l:%i %p'
) AS Created_formatted,
TIME_FORMAT(
SEC_TO_TIME(
TIMESTAMPDIFF(
SECOND,
pinknoise.Created,
CURRENT_TIMESTAMP() - INTERVAL '0' HOUR
)
),
'%Hh %im'
) AS elapsed,
(
SELECT
Count(*)
FROM
`pinknoise` AS tb
WHERE
tb.FirstName = pinknoise.FirstName
) AS CountRequests
FROM
pinknoise
WHERE
status = 'incoming'
ORDER BY
Created DESC
A much better performance would have a join:
SELECT
pinknoise.id,
pinknoise.status,
pinknoise.FirstName,
pinknoise.LastName,
pinknoise.Track,
pinknoise.KeyChange,
pinknoise.Version,
DATE_FORMAT(
CONVERT_TZ(
pinknoise.Created,
'+00:00',
'+1:00'
),
'%l:%i %p'
) AS Created_formatted,
TIME_FORMAT(
SEC_TO_TIME(
TIMESTAMPDIFF(
SECOND,
pinknoise.Created,
CURRENT_TIMESTAMP() - INTERVAL '0' HOUR
)
),
'%Hh %im'
) AS elapsed,
tabA.CountRequests
FROM
pinknoise
INNER JOIN
(
SELECT
Count(*) AS 'CountRequests',
FirstName
FROM
`pinknoise`
GROUP BY
FirstName
) tabA
ON
pinknoise.FirstName = tabA.FirstName
WHERE
status = 'incoming'
ORDER BY
Created DESC
Your subselect is returning 2 values in the select portion where it only expects one value. I'm guessing you are getting the FirstName with the intent of doing a join. If so, then try this:
SELECT
p.id,
p.status,
p.FirstName,
p.LastName,
p.Track,
p.KeyChange,
p.Version,
DATE_FORMAT(CONVERT_TZ(p.Created,'+00:00','+1:00'), '%l:%i %p') AS Created_formatted,
TIME_FORMAT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, p.Created, CURRENT_TIMESTAMP() - INTERVAL '0' HOUR)),'%Hh %im') AS elapsed,
cnt.CountRequests
FROM
pinknoise p
inner join (SELECT p.FirstName, Count(*) AS CountRequests FROM pinknoise p GROUP by p.FirstName) cnt on p.FirstName = cnt.FirstName
WHERE
p.status = 'incoming'
ORDER BY
p.Created DESC;
I have a table with two columns "date_from" and "date_to". When the user inputs a date i want to get the row where te picked date is between those two, however the years on those dates dont matter for me - only the months wich form something like seasons i have come to this :
SELECT `date_from` , `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT( `date_from` , '%c' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c' )
AND DATE_FORMAT( `date_to` , '%c' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c' )
AND DATE_FORMAT( `date_from` , '%e' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%e' )
AND DATE_FORMAT( `date_to` , '%e' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%e' )
This works in some cases but a season can start for example at november and
end at march and then my query doesent return the correct result.
Use BETWEEN, which doesn't care about the order of the arguments at the right:
SELECT `date_from`, `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT(STR_TO_DATE('2017-02-03', '%Y-%m-%d'), '%c')
BETWEEN DATE_FORMAT(`date_from`, '%c')
AND DATE_FORMAT(`date_to`, '%c')
I don't think there is any additional value in repeating this condition with the %e format.
UPDATED: Try this query it might work by splitting the days from the months you are creating too many possibilities for the answer to be.
SELECT `date_from` , `date_to`
FROM `language_courses_accommodation_periods`
WHERE DATE_FORMAT( `date_from` , '%c-%d' ) <= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c-%d' )
AND DATE_FORMAT( `date_to` , '%c-%d' ) >= DATE_FORMAT( STR_TO_DATE( '2017-02-03', '%Y-%m-%d' ) , '%c-%d' )
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.)
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");