473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

single SQL query that returns the most recent values

I'm currently working on a component tracking file and we are using an hour meter query to always have up to date information. My problem is that I need the hour meter query to display the reading of the most recent date for each equipment number.

My columns are:
EQUIP_NO
DATE_READING
CONDITION
LIFE_TO_DATE

I want a single SQL query that, given an EQUIP_NO, returns only the most recent DATE_READING, CONDITION and LIFE_TO_DATE. I have looked at a few posts about this but am still having trouble getting it to work.

Any help on this matter would be great. Thanks
Mar 6 '07 #1
5 13228
ronverdonk
4,258 Expert 4TB
The following query returns only 1 of the latest (newest date_reading) row.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table WHERE equip_no='xxxx' ORDER BY date_reading DESC LIMIT 1;
Ronald :cool:
Mar 6 '07 #2
The following query returns only 1 of the latest (newest date_reading) row.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table WHERE equip_no='xxxx' ORDER BY date_reading DESC LIMIT 1;
Ronald :cool:
This only returns the data for one machine because you have to specify the machine number. My query needs to return all 100 machines that we have and give the most recent reading of each machine. Say for example that the last time a reading was taken on DH031 was 2/25/2007 but most other machines have a reading on 3/7/2007. I need my query to be able to pick the most recent date reading for each machine and return the corresponding data.

In the full query (without an addition of code to SQL) there are readings for each date. But if a machine was parked or being worked on, then there will not be a reading for that day and the life to date hours would be the same. The problem here is that if there hasn't been a reading for 2 weeks for example, then that machine's last dated entry would be 2 weeks ago. It would not show up on the query if it's being filtered for 7/3/2007. Most code that i have tried to insert only shows values for the most recent reading of the entire data.

Here's a little example of the full hour meter reading query:

EQUIP_NO DATE_READING CONDITION LIFE_TO_DATE

DH028 3/7/2007 FTD 521400
DH028 3/6/2007 FTD 521200
DH028 3/5/2007 FTD 521000
DH028 3/4/2007 FTD 520750
and so on.... depending on how far you wanted to go back
DH031 2/27/2007 FTD 421030
DH031 2/26/2007 FTD 420900
DH031 2/25/2007 FTD 420500
and so on

So I need my query to return the most recent reading for each machine. As in this example, I would want

EQUIP_NO DATE_READING CONDITION LIFE_TO_DATE

DH028 3/7/2007 FTD 521400
DH031 2/27/2007 FTD 421030

But as i mentioned previously we have about 100 machines so would need 1 entry for the last time there was a reading for each machine. I hope this clarifies my problem and thank you to whoever lends a hand.
Mar 8 '07 #3
Bill

This should (i.e. is untested) do the trick...

Expand|Select|Wrap|Line Numbers
  1. select
  2. T1.EQUIP_NO
  3. T2.DATE_READING
  4. T2.CONDITION
  5. T2.LIFE_TO_DATE
  6. from
  7. (select distinct EQUIP_NO from MY_EQUIPMENT) as T1
  8. inner join MY_EQUIPMENT as T2
  9. on T1.EQUIP_NO=T2.EQUIP_NO
  10. and T2.DATE_READING=(select max(DATE_READING) from MY_EQUIPMENT where EQUIP_NO=T1.EQUIP_NO)
I found your post while trying to solve basically the same problem. I found my solution in the article titled "Only the latest matching row" here: [link removed]

Regards,
Nicolas
Apr 30 '07 #4
gisps
1
This only returns the data for one machine because you have to specify the machine number. My query needs to return all 100 machines that we have and give the most recent reading of each machine. Say for example that the last time a reading was taken on DH031 was 2/25/2007 but most other machines have a reading on 3/7/2007. I need my query to be able to pick the most recent date reading for each machine and return the corresponding data.

In the full query (without an addition of code to SQL) there are readings for each date. But if a machine was parked or being worked on, then there will not be a reading for that day and the life to date hours would be the same. The problem here is that if there hasn't been a reading for 2 weeks for example, then that machine's last dated entry would be 2 weeks ago. It would not show up on the query if it's being filtered for 7/3/2007. Most code that i have tried to insert only shows values for the most recent reading of the entire data.

Here's a little example of the full hour meter reading query:

EQUIP_NO DATE_READING CONDITION LIFE_TO_DATE

DH028 3/7/2007 FTD 521400
DH028 3/6/2007 FTD 521200
DH028 3/5/2007 FTD 521000
DH028 3/4/2007 FTD 520750
and so on.... depending on how far you wanted to go back
DH031 2/27/2007 FTD 421030
DH031 2/26/2007 FTD 420900
DH031 2/25/2007 FTD 420500
and so on

So I need my query to return the most recent reading for each machine. As in this example, I would want

EQUIP_NO DATE_READING CONDITION LIFE_TO_DATE

DH028 3/7/2007 FTD 521400
DH031 2/27/2007 FTD 421030

But as i mentioned previously we have about 100 machines so would need 1 entry for the last time there was a reading for each machine. I hope this clarifies my problem and thank you to whoever lends a hand.
Did you ever get an answer to your question? I'm having the same problem with no luck. Once I group other columns into the query it returns multiple records of the history. Max works fine for only one column.
May 23 '07 #5
Thank you for posting that, nicolasn. I have been searching for a solution for this issue for some time, and this is the only one that has worked.
Nov 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shilsum | last post by:
I would like to seek your expert opinions on the following problem of mine: I have a table to store employees Here is the partial structure of the table EMPID EMP_POS DATE MGMT
6
by: jim | last post by:
Hi, I have a site with various parts which allow users to enter info via forms which gets entered into various tables in a MySQL db (I'm using ASP, rather than PHP). I have an ecards bit, and...
4
by: Wanny | last post by:
Hi There, I can't seem to see what's wrong with the query below DELETE FROM Users_Details UD1 WHERE UD1.UserID = ( SELECT TOP 1 UD2.UserID FROM Users_Details UD2 WHERE UD1.useremail =...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
6
by: Toucan | last post by:
i need to retrieve the most recent timestamped records with unique names (see working query below) what i'm having trouble with is returning the next-most-recent records (records w/ id 1 and 3...
2
by: Andrew Stanton | last post by:
Hi, I have a query that returns a sum for all values found that match the criteria. I am wanting to place this value in a textbox on its own so I can refer to it elsewhere. I have used the...
3
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance you can provide. I have a table in an Access 2000 .mdb file, and I've run into something odd and insolvable, at least for me. The database is...
5
by: HoganGroup | last post by:
Hi Experts and Fellow Duffers: I am trying to create a query which will identify the most recent EndDate in tblSchedules associated with tblKids.KidID where a Null value (indicating a current...
2
by: markcarroll | last post by:
I have a rather complicating query (the SQL is about a page long) so I hope I can solve this without needing to get into specifics. Basically, the database I am working on has information about...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.