I'm tyring to use a where clause with a subquery inside a 'in'statement.
Thisis my query:
select *
from trading.historical_prices
where
date in (
(select date
from trading.historical_prices
group by date
order by date desc
limit 1)
union
(
select date
from trading.historical_prices
group by date
order by date desc
limit 7,1
)
)
limit 100
But I'm getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union
(
select date
from trading.historical_prices
group by date
orde' at line 13
The union query works fine when it's ran alone.
How can i fix this?
According to this answer, you don't parenthesis.
So your query would look like so :
SELECT *
FROM trading.historical_prices
WHERE date IN
(
SELECT date
FROM trading.historical_prices
GROUP BY date
ORDER BY date DESC limit 1
UNION
SELECT date
FROM trading.historical_prices
GROUP BY date
ORDER BY date DESC limit 7,
1) limit 100
Related
As the title says, I have a table with a date column. I am trying to retrieve all rows between 2 dates as well as X number of rows before the beginning date ordered by date.
Take a select * from table order by date DSC
20200201
20200101
20191201
20191101
20191001
20190901
20190801
20190701
I want between 20200201 and 20191201 and the previous 3 rows(not knowing the date)
result
20200201
20200101
20191201
20191101
20191001
20190901
my current query returns a random set of high dates between the union for some reason:
(SELECT * FROM Table WHERE Date BETWEEN 20200201 AND 20191201 ORDER BY Date ASC)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
Any idea where I am going wrong?
One method uses lead():
select t.*
from (select t.*,
lead(date, 3) over (order by date) as next_date_3
from t
) t
where (date < '202o-02-01' and (next_date_3 >= '2020-02-01' or next_date_3 is null) ) or
(date between '2020-02-01' and '2019-12-01')
I tried this query and it worked for me:
(SELECT * FROM Table WHERE Date BETWEEN 20191201 AND 20200201 ORDER BY Date ASC)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
The first select shows me:
20191201
20200101
20200201
The second select shows me:
20191101
20191001
20190901
The issue was as jarlh pointed out, the order of the subquery's were not kept(thats why things were jumbled) and a simple ORDER BY of the whole UNION fixed this. I did not try Gordon's response as this solved my issue so:
(SELECT * FROM Table WHERE Date BETWEEN 20191201 AND 20200201)
UNION
(SELECT * FROM Table WHERE Date < 20191201 ORDER BY Date DESC LIMIT 3)
ORDER BY Date ASC
Thank you
My select command:
select * from wp_ved_currencies where date in (select DISTINCT date from wp_ved_currencies order by date desc limit 2);
Error message:
Error
SQL query:Documentation
select * from wp_ved_currencies where date in (select distinct date from wp_ved_currencies order by date desc limit 2) LIMIT 0, 50
MySQL said:Documentation
#1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Could you help me here?
You should be able to join to a limit subquery here:
SELECT w1.*
FROM wp_ved_currencies w1
INNER JOIN
(
SELECT DISTINCT date
FROM wp_ved_currencies
ORDER BY date DESC
LIMIT 2
) w2
ON w1.date = w2.date;
I have a database that stores the temperature reading of each sensor in a along with the sensor ID, and date the reading was taken.
SELECT DISTINCT `date` FROM `temperatureData` ORDER BY `date` ASC LIMIT 10
this allows me to select the last 10 readings that are going to be plotted in a chart.
there are up to 40 sensor readings for each date.
I tried doing the following.
SELECT `date`, `sensor`, `temp`
FROM `temperatureData`
WHERE `date` = (
SELECT DISTINCT `date` FROM `temperatureData` ORDER BY `date` ASC LIMIT 10
)
Can anyone assist me as to how to select all the readings for the dates that are returned back from the last 10 dates?
Thanks in advance.
Boris
You just need in instead of =:
SELECT `date`, `sensor`, `temp`
FROM `temperatureData`
WHERE `date` IN (
SELECT DISTINCT `date` FROM `temperatureData` ORDER BY `date` DESC LIMIT 10
)
NB: if you want the readings of the last 10 dates, you probably want to ORDER BY date DESC instead of ASC. I changed that too.
In MySQL 8.0, this could also be rewritten with window function dense_rank():
SELECT `date`, `sensor`, `temp`
FROM (
SELECT
`date`,
`sensor`,
`temp`
DENSE_RANK() OVER(ORDER BY `date` DESC) rn
FROM `temperatureData`
) t
WHERE rn <= 10
Edit
To workaround the limitation of MySQL 5.7 not supporting LIMIT in subqueries with IN, you can use a join instead:
SELECT t.`date`, t.`sensor`, t.`temp`
FROM `temperatureData` t
INNER JOIN (
SELECT DISTINCT `date` FROM `temperatureData` ORDER BY `date` DESC LIMIT 10
) d ON d.`date` = t.`date`
I have a table with some events like this
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
2-----------match----------2018-03-13--------2
3-----------anniversary----2018-03-10--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
6-----------birthday-------2018-03-11--------1
Expected Result
id----------title-----------date-------------status
1-----------birthday-------2018-03-12--------1
4-----------trip-----------2018-03-15--------1
5-----------birthday-------2018-03-17--------2
2-----------match----------2018-03-13--------2
6-----------birthday-------2018-03-11--------1
3-----------anniversary----2018-03-10--------1
I need to query it like the first rows which have dates greater than today with status 1 should appear first and then the rest in desc.
Suppose today is 2018-03-11 then row with id 1 should appear first and then the rest of the rows is desc order
This is what I have tried so far
SELECT *
FROM events
ORDER BY (date > CURDATE() and status = 1) asc,
date desc
You can use multiple keys in an order by:
order by (date >= curdate() and status = 1) desc,
date desc
I believe your SQL should be something like this but is hard to say without expected results.
Query
SELECT
*
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
ORDER BY
date ASC
LIMIT 1
UNION
SELECT
*
FROM
[table]
WHERE
id NOT IN (
SELECT
id
FROM
[table]
WHERE
date > CURDATE()
AND
status = 1
LIMIT 1
)
AND
date > CURDATE()
ORDER BY
date DESC
My current table looks like this - Table is called "S"
DJ DATE
---- ------
test yyyy/mm/dd hh:mm:ss
I've worked up to the below SQL query
SELECT *
FROM `S`
WHERE `DATE` = '2017-02-27 17:00:00'
ORDER BY `S`.`DATE` DESC
LIMIT 0 , 1
Rather than having the set date and time though I would like to query based on the current date AND time. I'm using datetime type structure.
All I'm looking to do is query the current datetime to get the current name from the DJ row
Thanks!
You can use now() function to get the current datetime.
SELECT *
FROM `S`
WHERE `DATE` = now()
ORDER BY `S`.`DATE` DESC
LIMIT 0 , 1
If you want to get result for today's date, you can use curdate and date function
SELECT *
FROM `S`
WHERE date(`DATE`) = curdate()
ORDER BY `S`.`DATE` DESC
LIMIT 0 , 1
With mysql, you can use the CURRENT_TIMESTAMP() function.
SELECT *
FROM S
WHERE DATE = CURRENT_TIMESTAMP()
ORDER BY S.DATE DESC
LIMIT 0, 1
I think you want:
select s.*
from s
where date(date) = CURDATE() and
hour(date) = hour(NOW())
order s.date` DESC
limit 0, 1;