Select the lastest N day data from table - sql

price date time
1.0 20100815 1
1.2 20100815 2
1.3 20100815 3
2 20100814 1
3.1 20100813 1
3.2 20100813 2
:
:
:
Now I want to select the latest 3 days price with all the time, I use like this
select price, date from allquotes where date in
(select date from allquotes group by date order by date desc limit 3)
Is this right? Is this efficient?
Any suggestion to improve this?
If I would like to show only one price with the latest time, how to do that?
Thanks so much!

This should do the trick on SQL Server:
select top 3 q.pricee, q.date, q.time
from (
select date, max(time) as MaxTime
from allquotes
group by date
) qm
inner join quotes q on qm.date = q.date and qm.MaxTime = time
order by date desc
For MySQL, try:
select q.pricee, q.date, q.time
from (
select date, max(time) as MaxTime
from allquotes
group by date
) qm
inner join quotes q on qm.date = q.date and qm.MaxTime = time
order by date desc
limit 3

Select Top 3 Sum(price),
date,
Sum(time)
From allquotes
Group By date
Order By date Desc

Related

how to get unique row numbers in sql

How to get only the first row from the result of the below query. I need the latest record for each date so I did the partition by created_date. But in some places, I am getting the same row number and not able to get the expected output. Please find the below query, current output, and expected output.
What changes do in need to make in order to get the expected output? Thank you.
WITH ctetable
AS (
SELECT created_date BPMDate
,tenor
,row_number() OVER (
PARTITION BY created_date ORDER BY created_date DESC
) rw
FROM table1 a
INNER JOIN table2 b ON a.case_id = b.case_id
AND a.eligible_transaction = 'true'
AND to_date(a.created_date) >= '2020-10-01'
AND to_date(a.created_date) <= '2020-10-05'
AND case_status = 'Completed'
)
SELECT BPMDate
,Tenor
,rw
FROM ctetable
Current output:
date tenor rw
2020-10-05 13:24:15.0 1W 1
2020-10-05 12:15:43.0 1Y 1
2020-10-05 12:15:43.0 1Y 2
2020-10-01 13:30:59.0 1W 1
2020-10-01 13:30:59.0 1W 2
Expected output:
date tenor rw
2020-10-05 13:24:15.0 1W 1
2020-10-01 13:30:59.0 1W 1
Regards,
Viresh
That would be:
with ctetable as (
select created_date, bpmdate, tenor,
row_number() over (partition by date(created_date) order by created_date desc ) rn
from table1 a
inner join table2 b
on a.case_id = b.case_id
and a.eligible_transaction = 'true'
and to_date(a.created_date) >= '2020-10-01'
and to_date(a.created_date) <= '2020-10-05'
and case_status='completed'
)
select bpmdate,tenor,rw
from ctetable
where rn = 1
Changes to your original code:
you need to remove the time portion of the date in the partition by clause of the window function; you didn't tell which database you are using: I used date(), but the function might be different in your database (trunc() in Oracle, date_trunc() in Postgres, and so on)
the outer query needs to filter on the row number that is equal to 1
You seem to want the first row per day:
select BPMDate, Tenor, rw
from (select t.*,
row_number() over (partition by trunc(bpmdate) order by bpmdate) as seqnum
from ctetable
) t
where seqnum = 1;
Note: I don't know if your database supports trunc(), but that is simply some method for extracting the date from the column.

How do I get all rows from the second to latest date?

I have gotten all rows for the latest date like this:
SELECT date, quarter, sales_region, revenue
FROM regions
WHERE date = (SELECT MAX(date) FROM regions)
ORDER BY 1
So how would I get the rows for the second latest date?
I have tried but no luck:
SELECT MAX(date), quarter, sales_region, revenue
FROM regions
WHERE date < (SELECT MAX(date) FROM regions)
ORDER BY 1
Here is one method:
SELECT date, quarter, sales_region, revenue
FROM regions
WHERE date = (SELECT DISTINCT date
FROM regions r2
ORDER BY date DESC
OFFSET 1 FETCH FIRST 1 ROW ONLY
)
ORDER BY 1;
Another method uses dense_rank():
select r.*
from (select r.*, dense_rank() over (order by date desc) as seqnum
from regions r
) r
where seqnum = 2;
Gordon answered your question precisely, but if you want to get the records for the last two dates in one query, you could use IN instead of =, and get the top two records with LIMIT 2:
SELECT date, quarter, sales_region, revenue
FROM regions
WHERE date IN (SELECT DISTINCT date
FROM regions r2
ORDER BY date DESC
LIMIT 2)
ORDER BY 1;
Starting with version 8.4, you can also use FETCH FIRST 2 ROW ONLY instead of LIMIT 2.

Valid price at given date

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.

how to get last date form DB table mysql

i have this table in my DB
categoriesSupports-> id, category_id, support_id, date
the thing is that i need to extract all support_id where date is the closest date from now...
something like this... if there is in the DB table
id, category_id, support_id, date
1 1 1 2010-11-23
2 1 2 2010-11-25
3 1 1 2010-11-26
4 1 3 2010-11-24
i need to get just
id, category_id, support_id, date
2 1 2 2010-11-25
3 1 1 2010-11-26
4 1 3 2010-11-24
So for better undestanding... i need the closest date for each support and i only have date from the past...
Ive being trying a lot and I dont know how...
The following should give you:
all the categoriesSupports for current date(one or multiple)
One previous categoriesSupport(if exists)
One future categoriesSupport(if exists)
(
SELECT *
FROM `categoriesSupports`
WHERE `date` < CURDATE()
ORDER BY `date` DESC
LIMIT 1
)
UNION
(
SELECT *
FROM `categoriesSupports`
WHERE `date` = CURDATE()
)
UNION
(
SELECT *
FROM `categoriesSupports`
WHERE `date` > CURDATE()
ORDER BY `date` ASC
LIMIT 1
)
A. This answers 'where date is the closest date from now...':
SELECT *
FROM `categoriesSupports`
WHERE `date` IN (
SELECT `date`
FROM `categoriesSupports`
ORDER BY `date` DESC
LIMIT 1
)
Notes:
You can set LIMIT n to select entries for more dates.
If you only want for the last date you can replace IN with = because the sub-select will return only one value.
If your table includes future dates replace ORDER BY date DESC with ORDER BY ABS(NOW() - date) ASC.
A solution with JOINS. Will work only if you have past dates.
SELECT a.*
FROM `categoriesSupports` AS a
LEFT JOIN `categoriesSupports` AS b
ON b.date > a.date
WHERE b.id IS NULL
Added just for reference.
B. This answers 'where date is in the last 3 days (including today)':
SELECT *
FROM `categoriesSupports`
WHERE DATEDIFF(NOW(), `date`) < 3
Replace 3 with any number if you want more or less days.
C. Same as A., but per support id
SELECT a.*
FROM `categoriesSupports` AS a
LEFT JOIN `categoriesSupports` AS b
ON b.support_id = a.support_id AND b.date > a.date
WHERE b.id IS NULL
This answers the latest version of the question.
SELECT *
FROM CurrentDeals
WHERE (julianday(Date('now')) - julianday(date))<=3
ORDER BY date ASC
Here, you have to decide what would be your meaning of "closest". I have used 3 as the sample. This will list out the records, which has a date value lesser that or equal to 3.
Hope this is what you wanted.

How to do this query?

I have this mysql table:
id - auto_increment
id_stock - int
price - double
date - date
sample data is:
1 1 10.5 2010-08-10
2 1 16.5 2010-08-11
3 2 12.5 2010-08-12
now, i have to group by id_stock and search for the MAX(date) of the stock, then i have to compare the MAX(date) to a date i have to pass.
How to do it?
Thank you really much
SELECT ...
FROM Table
GROUP BY Id_Stock
HAVING Max(Date) = YourPassedDate
You should be able to get this using 'group' and 'having' together:
select id, MAX(date) as max_date from
test group by id_stock having max_date > '2010-08-11'