got a table with dates and prices.
Date Price
2012-01-01 25
2012-01-05 12
2012-01-10 10
Is there some kind of function that lets me find what the current price where at '2012-01-07'? Without me knowing of the other dates.
Pseudoquery: select price where currentprice('2012-01-07')
Thanks!
MySQL:
select price from your_table
where date <= '2012-01-07'
order by date desc
limit 1
SQL Server:
select top 1 price from your_table
where date <= '2012-01-07'
order by date desc
If you don't have use of ROW_NUMBER(), and want a generic solution, you need to join on a sub-query.
Get the date you want, then get the data for that date.
SELECT
*
FROM
yourTable
INNER JOIN
(
SELECT MAX(yourDate) AS maxDate FROM yourTable WHERE yourDate <= #dateParameter
)
AS lookup
ON yourTable.yourDate = lookup.maxDate
select price
from table1 t
where t.date = ( select max(t2.date)
from table1 t2
where t2.date <= '2012-01-07' )
Note this is not the copy&paste answer, as we're not not knowing what is the datatype for your date column.
Related
I am trying to perform an analysis where I select from a table only rows that fulfill certain criteria. In this instance, I am interested in date criteria. Specifically, in this query:
SELECT * FROM INPUT_TABLE
WHERE THE_DATE<='2018-01-01' and THE_DATE >='2017-01-01'
I wish to replace the strings '2018-01-01' and '2017-01-01'
with a sort of subquery, where I keep the values of the minimum and maximum dates in another table,called VALUE_TABLES which has the following 2 values:
MAX_DATE MIN_DATE
2018-01-01 2017-01-01
How exactly can I do this?
Use JOIN:
SELECT *
FROM INPUT_TABLE i JOIN
(SELECT MIN(DATE) as MIN_DATE, MAX(DATE) as MAX_DATE
FROM TABLE2
) t2
ON i.THE_DATE >= T2.MIN_DATE AND i.THE_DATE <= t2.MAX_DATE;
I've written some code to extract the latest date from multiple columns.
select (select max(LatestDate)
from (values (col1),(col2),(col3)) as updatedate(LatestDate)
) as LatestDate
from table1
However, I only want to take the date if it's before today. When I run the code for the sample dates below, it gives me the latest date as 10/04/2019 which is after today.
The date that i'd want it to extract is 14/03/2019 (col2) as it's before today, and is the latest date of all the columns whose date is before today.
Today = 27/03/2019
col1 = 02/02/2019
col2 = 14/03/2019
col3 = 10/04/2019
Can anyone advise on this? Hope it makes sense.
Many thanks
afk
You can use APPLY with WHERE clause :
select t.*, tt.LatestDate
from table1 t outer apply
( select max(LatestDate) as LatestDate
from ( values (col1),(col2),(col3) ) as updatedate(LatestDate)
where LatestDate < convert(date, GETDATE())
) tt;
You can use the below code for achieving the same.
select (select max(LatestDate)
from (values (col1),(col2),(col3)) as updatedate(LatestDate)
where updatedate < CAST(GETDATE() AS DATE)
) as LatestDate
from table1
Try this - you can take the MAX date that is less than or equal to today:
with cte as
(
select *
from (values (col1),(col2),(col3)) as updatedate
)
select (
select max(updatedate)
from cte
where updatedate <= GETDATE()
) as LatestDate
from table1
Add a where clause. I would phrase the query using cross apply:
select max(LatestDate)
from table1 t1 cross apply
(values (t1.col1), (t1.col2), (t1.col3)
) updatedate(LatestDate)
where updateddate.LatestDate < getdate();
I'm trying the find a value at a certain date.
My data looks like
Date Value
2013-11-02 5
2013-10-10 8
2013-09-14 6
2013-08-15 4
How can I determine what the value was on 2013-09-30?
Obviously the answer is 6 but I can't figure out the SQL code.
Thanks
You can do it with order by and limiting the number of rows. In SQL Server syntax (and Sybase and Access):
select top 1 t.*
from table t
where date <= '2013-09-30'
order by date desc;
In MySQL (and Postgres):
select t.*
from table t
where date <= '2013-09-30'
order by date desc
limit 1;
In Oracle:
select t.*
from (select t.*
from table t
where date <= '2013-09-30'
order by date desc
) t
where rownum = 1
EDIT:
And, a SQL standard way to it (should work in any database):
select t.*
from table t
where date = (select max(date)
from table t2
where date <= '2013-09-30'
);
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
GROUP BY date, id
HAVING sum(revenue)>1000
Returns rows that have revenue>1000.
SELECT date, id, sum(revenue)
FROM table
WHERE date between '2013-01-01' and '2013-01-08'
AND id IN (SELECT id FROM table where date between '2013-01-01' and '2013-01-08' GROUP BY id HAVING sum(revenue)>1000)
GROUP BY date, id
Returns rows for id's whose total revenue over the date period is >1000 as desired. But this query is much slower. Any quicker way to do this?
Make sure you have indexes on the date and id columns, and try this variation:
select t.date, t.id, sum(t.revenue)
from table t
inner join (
select id
from table
where date between '2013-01-01' and '2013-01-08'
group by id
having sum(revenue) > 1000
) ts on t.id = ts.id
where t.date between '2013-01-01' and '2013-01-08'
group by t.date, t.id
it's not MySQL, it's Vertica ;)
Cris, what projection and order by you using in CREATE TABLE ???
Do you try using database designer
see http://my.vertica.com/docs/6.1.x/HTML/index.htm#14415.htm
I have four dates with a different prices for each date:
10/01/2011 $25
10/08/2011 $50
11/17/2011 $100
12/23/2011 $150
SQL:
SELECT price FROM MyTable WHERE MyDate <= '10/12/2011'
PROBLEM: This query returns $25 and $50. I need it to give me the nearest date only...
How can i have it return only the $50?
SELECT top 1 price FROM MyTable WHERE MyDate <= '10/12/2011' order by MyDate desc
Try this (in SQL Server)
SELECT TOP 1 price
FROM MyTable
WHERE myDate <= getDate()
ORDER BY myDate DESC
Try this (in mySQL)
SELECT price
FROM MyTable
WHERE myDate <= now()
ORDER BY myDate DESC
LIMIT 1