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'
Related
How to extract the difference of a specific column of multiple rows with same id?
Example table:
id
prev_val
new_val
date
1
0
1
2020-01-01 10:00
1
1
2
2020-01-01 11:00
2
0
1
2020-01-01 10:00
2
1
2
2020-01-02 10:00
expected result:
id
duration_in_hours
1
1
2
24
summary:
with id=1, (2020-01-01 10:00 - 2020-01-01 11:00) is 1hour;
with id=2, (2020-01-01 10:00 - 2020-01-02 10:00) is 24hour
Can we achieve this with SQL?
This solutions will be an effective way
with pd as (
select
id,
max(date) filter (where c.old_value = '0') as "prev",
max(date) filter (where c.old_value = '1') as "new"
from
table
group by
id )
select
id ,
new - prev as diff
from
pd;
if you need the difference between successive readings something like this should work
select a.id, a.new_val, a.date - b.date
from my_table a join my_table b
on a.id = b.id and a.prev_val = b.new_val
you could use min/max subqueries. For example:
SELECT mn.id, (mx.maxdate - mn.mindate) as "duration",
FROM (SELECT id, max(date) as mindate FROM table GROUP BY id) mn
JOIN (SELECT id, min(date) as maxdate FROM table GROUP BY id) mx ON
mx.id=mn.id
Let me know if you need help in converting duration to hours.
You can use the lead()/lag() window functions to access data from the next/ previous row. You can further subtract timestamps to give an interval and extract the parts needed.
select id, floor( extract('day' from diff)*24 + extract('hour' from diff) ) "Time Difference: Hours"
from (select id, date_ts - lag(date_ts) over (partition by id order by date_ts) diff
from example
) hd
where diff is not null
order by id;
NOTE: Your expected results, as presented, are incorrect. The results would be -1 and -24 respectively.
DATE is a very poor choice for a column name. It is both a Postgres data type (at best leads to confusion) and a SQL Standard reserved word.
I have a table in Postgres with timestamps:
timestamp
2022-01-01 00:52:53
2022-01-01 00:57:12
...
2022-02-13 11:00:31
2022-02-13 16:45:10
How can I select the timestamp closest to max timestamp? Meaning, I want the timestamp 2022-02-13 11:00:31.
I am looking for something like max(timestamp)-1 so I can do on a recurring basis. Thank you
You can do:
select *
from (
select *,
rank() over(order by timestamp desc) as rk
from t
) x
where rk = 2
See running example at DB Fiddle.
I think the following query might meet your requirements:
SELECT MAX(date_col) FROM test WHERE date_col < (SELECT MAX(date_col) from test);
See DB Fiddle
Could I kindly ask for some guidance on how I can filter in Presto SQL only by the values (from column a) that have all values in column b?
So, I am looking to get all the product_ids by date that have all promotion days (from 1 - 9) in promotion_running_days column.
I tried to use 'promotion_running_days in (1,2,3,4,5,6,7) but it returns also the product_ids have only 2 or 3 promotion days.
Using this query approach:
SELECT
product_id
,date
,ROUND(MAX(DATE_DIFF('day', CAST(DATE_PARSE(promotion_start_date, '%Y-%m-%d %T') AS DATE), CAST(DATE_PARSE(date, '%Y-%m-%d') AS DATE))),0) AS promotion_running_days
,SUM(revenue) AS total_revenue
FROM product_db
WHERE
date between '2019-01-01' and '2019-01-07'
AND promotion_start_date>='2019-01-01'
Group by 1,2;
I would like my output to look like this:
Product Id |Date| |Promotion Running Days|
1 |2019-01-01| |1|
1 |2019-01-02| |2|
1 |2019-01-03| |3|
1 |2019-01-04| |4|
1 |2019-01-05| |5|
1 |2019-01-06| |6|
1 |2019-01-07| |7|
I am looking to get all the product_ids by date that have all promotion days
You seem to want aggregation. Assuming you have at most one row per date:
SELECT product_id, SUM(revenue) AS total_revenue
FROM product_db
WHERE date between '2019-01-01' and '2019-01-07' and
promotion_start_date>='2019-01-01'
GROUP BY product_id
HAVING COUNT(*) = 7; -- 7 == all days
However, your sample results suggest row_number():
select product_id, date,
row_number() over (partition by product_id order by date)
from product_db
order by product_id, date;
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.
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