Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How to retrieve latest value for each card from time from the table in SQL including sensor id and event type closest to todays date please?
create table events (
sensor_id integer not null,
event_type integer not null,
value integer not null,
time timestamp unique not null
);
I have tried code below
SELECT TOP 1 *
FROM events
WHERE events.time < "2021-01-01 00:00:01"
ORDER BY events.time DESC
Top 1 does not work in mysql and limit 1 did not get me the results.
I need to get latest time value for each sensor_id not only the latest value for the whole table.
You can use order by and limit. Let me assume that you mean on or before today:
select e.*
from events e
where e.timestamp <= now()
order by e.timestamp desc
limit 1;
If you want future dates as well, this can be tweaked.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get lastupdate datetime order by updateTime but prevent for redundant date in my output
desired result
2021-06-25 15:46:57
2021-06-26 15:48:52
2021-06-27 17:11:52
2021-06-28 17:17:33
2021-06-29 15:16:29
I tried this
SELECT t.updateHistoryID, t.updateTime
FROM web_historyupdate t
INNER JOIN ( SELECT updateHistoryID, max(updateTime) as maxdate
FROM web_historyupdate
GROUP BY updateHistoryID ) tm
ON t.updateHistoryID =tm.updateHistoryID
AND t=tm.maxdate
You want the maximum datetime per date. As mentioned, "Maximum" translates to MAX in SQL and "per" transates to GROUP BY. One way to apply this to your data:
select *
from web_historyupdate
where updateTime in
(
select max(updateTime)
from web_historyupdate
group by date(updateTime)
)
order by updateTime;
There exist of course other ways to do this. You could for instance select every row for which NOT EXISTS a row on the same day at a later time.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a daily stock transaction table
T1(symbol, transDate, closingPrice, PrevQtrChange).
The last column is empty. I need an update statement that, for a given symbol, will get the closing price from the previous quarters transaction. Because of weekends, holidays, etc, i can't do a self join on the date being date-90 days. I could do it with a cursor, but ugh. And, the table contains millions of rows, so a cursor would be extremely slow, even with an index.
I'm a C/C++ programmer so while I know some SQL, doing this efficiently is something I'm unsure of.
Thanks in advance.
You can use window functions. The idea for the previous price is:
select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t;
You can then incorporate this into an update:
update t join
(select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t
) tt
on tt.symbol = t.symbol and tt.transDate = t.transDate
set t.PrevQtrChange = closingprice - tt.prev_quarterprice
where tt.PrevQtrChange is null ;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have following table to monitor the price each day, and I want to do calculation between two days like today's close price - previous day
s open price , e.g Thur's close - Wed's open, so I can see the difference.
Also, there is no records for the weekend, so how I can do 2 calculation different,
e.g 1. Mon's close - Sun returns same Mon price,
2. Mon's close price - previous Fri's open etc
INSERT INTO goods(date,day,open,high,low,close,`range(daily high- low)`) VALUES
('2018-11-01','Thu',1.08430,1.08766,1.08175,1.08285,0.00591),
('2018-11-02','Fri',1.08319,1.08585,1.07988,1.07988,0.00597),
('2018-11-05','Mon',1.08258,1.08389,1.08011,1.08155,0.00378),
('2018-11-06','Tue',1.08160,1.08489,1.07461,1.07469,0.01028),
('2018-11-07','Wed',1.07543,1.07646,1.07094,1.07150,0.00552),
('2018-11-08','Thu',1.07148,1.07571,1.07083,1.07393,0.00488),
('2018-11-09','Fri',1.07409,1.07651,1.07124,1.07125,0.00527),
('2018-11-12','Mon',1.07190,1.07389,1.06759,1.06878,0.00630),
('2018-11-13','Tue',1.06830,1.06977,1.06609,1.06658,0.00368)
Ideal output 1:
date, day, open, high, low,close, diff
('2018-11-01','Thu',1.08430,1.08766,1.08175,1.08285, ..
('2018-11-02','Fri',1.08319,1.08585,1.07988,1.07988, - 0.00442
('2018-11-05','Mon',1.08258,1.08389,1.08011,1.08155,- 0.00164
('2018-11-06','Tue',1.08160,1.08489,1.07461,1.07469,-0.00789
output 2:
date, day, open, high, low,close, diff
('2018-11-01','Thu',1.08430,1.08766,1.08175,1.08285, ..
('2018-11-02','Fri',1.08319,1.08585,1.07988,1.07988, - 0.00442
('2018-11-05','Mon',1.08258,1.08389,1.08011,1.08155, 1.08155
('2018-11-06','Tue',1.08160,1.08489,1.07461,1.07469,-0.00789
I am using php7.1, mysql
If you are using MySQL 8.0, you can use LAG() to access the immediate previous record, ordered by date. This will happily ignore gaps in days, so Monday closing price will be compared with Friday openning price :
SELECT
g.*,
g.close - LAG(g.open) OVER(ORDER BY g.date) price_diff
FROM goods g
Demo on DB Fiddle
With older versions of MySQL, one would typically use a self-join and a correlated subquery with a NOT EXISTS condition to retrieve the previous record :
SELECT g.*, g.close - g1.open price_diff
FROM goods g
LEFT JOIN goods g1
ON g1.date < g.date
AND NOT EXISTS (
SELECT 1 FROM goods g2 WHERE g2.date < g.date AND g2.date > g1.date
)
g is the current record (say : today). g1 represents yesterday’s record : to identify it, we indicate the RDBMS that :
g1's date is lower than g's date
no record (g2) exists with a date lower than today’s (g) and higher than yesterday’s (g1)
The combination of these two conditions allows the RDMS to uniquely identify the relevant record (yesterday’s), whose value can then be used in the computation.
Demo on DB Fiddle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I have a table of entries with a startdatetime and enddatetime, I want to be able to see overlap like employee coverage. For instance I have 10 employees on shift with different start times and end times and I want to be able to query by hour and see how many employees are available per hour.
I had not tried because I did not find a place to start, I do something similar when they is incoming tasks at a specific time to see how many in each hour, but I am trying to see coverage in each hour of the day based on when an employee starts and ends, like 8 hour shift, 12 hours shift, etc.. So That I can see that I have 2, 3, 4, etc... coverage and map to number of tasks that come in and are planned. Something below looks promising but its only for 1 time for each query, would like a query that can bring back each hour and how many cover in each hour.
Any help would be great.
Try something like this:
SELECT count(*) as TOTAL
FROM table
WHERE '2015-05-28 12:00:00'
BETWEEN startTime and endTime
Obviously you need to change this '2015-05-28 12:00:00' with the desidered date/time you're looking for.
Add others condition if needed...
EDIT
Adding the extra request:
Grouping by date and hour gives you the total count of coverage in a single hour per day.
SELECT date(date_start) as DAY, hour(date_start) as HOUR, count(*) as TOTAL
FROM table
group by date(date_start), hour(date_start)
You can abviously add one or more condition.
ie. in this example below you'll get the total rows per hour in a single day (specified)
SELECT date(date_start) as DAY, hour(date_start) as HOUR, count(*) as TOTAL
FROM table
WHERE date('2015-05-28') = date(startTime)
group by date(date_start), hour(date_start)