Query for get lastupdate datetime in redudant date table [closed] - mysql

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.

Related

How to write query to retrieve latest time value closest to today in SQL? [closed]

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.

How do I count an instance based on 2 conditions that need to be met? [closed]

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 created a schema in MySQL workbench & a table named: "exam"
The table has column names: "Exam_ID", "Start_Time", "Finish_Time", & "Room_ID"
I want to count all the exams that start after 5pm corresponding to each room.
SELECT COUNT(`Start_Time`) FROM `exam` WHERE `Start_Time`> '17:00:00' AND `Room_ID` = 'IR1';
SELECT COUNT(`Start_Time`) FROM `exam` WHERE `Start_Time`> '17:00:00' AND `Room_ID` = 'IR2';
SELECT COUNT(`Start_Time`) FROM `exam` WHERE `Start_Time`> '17:00:00' AND `Room_ID` = 'IR2';
Here is a sample of what I desire the output to look like
How can I count the # of exams correctly based on the conditions
This looks like filtering and aggregation:
select room_id, count(*) as cnt
from exam
where start_time > '17:00:00'
group by room_id

same table update from previous quarter row [closed]

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 ;

mysql retrieve data from a range of time from table [closed]

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 6 years ago.
Improve this question
-this is the table of picture i want to retrieve datas from
I need two queries.
the first one is to get the actionNo of the latest stopTime before the current time.
the second query is to get the actionNo if the latest actionTime before the current time.
Example:
If the current time was: 00:46:20
the first query is to get actionNo 9 (stoptime: 00:45:00)
the second query is to get actionNo 8 (actiontime: 00:41:30)
-this is eg of range to get data picture
SELECT MAX(id) FROM timestuff WHERE `actionTime` < CURTIME();
SELECT MAX(id) FROM timestuff WHERE `stopTime` < CURTIME();
This gets you the latest id of the action/stoptime before the current time, will work if your table always is in the order as you have shown (that means, ids display the chronology of your time values - which isn't the case in your data as I just noticed).
For your case: you can take another step and add a inner join on the time closest to the current time and then look up the id of that data.
First query: For the latest stopTime before the current time:
SELECT
timestuff.id
FROM
(
SELECT
MAX(stopTime) AS maxTime
FROM
timestuff
WHERE
`stopTime` < CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`stopTime` = time.maxTime;
Second query: For the first actionTime after the current time:
SELECT
timestuff.id
FROM
(
SELECT
MIN(actionTime) AS minTime
FROM
timestuff
WHERE
`actionTime` > CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`actionTime` = time.minTime;

Oracle - Return rows with max_date duplicate [closed]

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 8 years ago.
Improve this question
I have the following table structure:
car_id number PK, rent_date date, return_date date.
The table is very inconsistent and I would like to retrieve the car_id,max(return_date) but only if the max(return_date) is duplicated. Is it possible?
For example:
2 12/12/12 13/12/12
2 11/12/12 13/12/12
The return_date is duplicated for the car_id 2 so it should be returned by the query.
Thanks !!
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, T.return_date
Having COUNT(*) > 1
If Time part of Date is not desired in comparison, you may use:
In Oracle:
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, TRUNC(T.return_date)
Having COUNT(*) > 1
In MySql:
SELECT T.car_id, max(T.return_date) From TableName T
Group by T.car_id, DATE(T.return_date)
Having COUNT(*) > 1