Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: SQL Join or Subquery
Or with the sensor name included
select t1.sensor_id,s.naam,t1.temp_c
from temperature t1, sensor s
where t1.timestamp = (select max(t2.timestamp)
from temperature t2 where t1.sensor_id=t2.sensor_id)and s.sensor_id=t1.sensor_id
11:30:15 29-CC1-JACK> select * from temperature 11:30:25 2 /
SENSOR_ID TEMP_C TIMESTAMP
---------- ---------- -----------------
1 15 18-jul-2002 09:25 2 16 18-jul-2002 09:25 1 11 18-jul-2002 09:25 2 18 18-jul-2002 09:25 3 17 18-jul-2002 09:25 1 15 18-jul-2002 09:25 2 15 18-jul-2002 09:25 3 15 18-jul-2002 09:25 1 11 18-jul-2002 09:25 2 12 18-jul-2002 09:25 3 110 18-jul-2002 09:25 2 12 18-jul-2002 09:30 3 110 18-jul-2002 09:30 1 11 18-jul-2002 09:30
14 rows selected.
11:30:26 29-CC1-JACK> select * from sensor 11:30:53 2 /
SENSOR_ID NAAM
---------- ------------------------------
1 sensor1 2 sensor2 3 sensor3
11:30:54 29-CC1-JACK> @xx
SENSOR_ID NAAM TEMP_C
---------- ------------------------------ ----------
1 sensor1 11 2 sensor2 12 3 sensor3 110
11:31:02 29-CC1-JACK>
"Jack van Zanen" <nlzanen1_at_ey.nl> To: Multiple recipients of list ORACLE-L <ORACLE-L_at_fatcity.com> Sent by: cc: (bcc: Jack van Zanen/nlzanen1/External/MEY/NL) root_at_fatcity.com Subject: Re: SQL Join or Subquery 18-07-2002 10:38 Please respond to ORACLE-L
Hi
Try:
select t1.sensor_id,t1.temp_c
from temperature t1
where t1.timestamp = (select max(t2.timestamp)
from temperature t2 where t1.sensor_id=t2.sensor_id)
Jack
Gary Chambers <gc22_at_lucent.com> To: Multiple recipients of list ORACLE-L <ORACLE-L_at_fatcity.com> Sent by: cc: (bcc: Jack van Zanen/nlzanen1/External/MEY/NL) root_at_fatcity.com Subject: SQL Join orSubquery
17-07-2002 23:54 Please respond to ORACLE-L
All...
I have a question about a query over which I've been racking my brain for weeks, and I can't find a reference to adequately assist me.
Given a table of temperature sensors, and a table of readings from those sensors, how might I grab the max (hence latest or current) temperature reading for each sensor? The relevant fields in the tables are timestamp and temp_c (temperature table), and sensorid (both temperature and sensor tables).
I'm fairly certain that I'm overlooking something very elementary, and I feel it should be simple and straightforward, but I think I'm being stymied by the use of aggregate functions in my query. I can provide examples of my failed attempts, if you'd like. Thanks in advance for any assistance you can provide.
Gary Chambers
//------------------------------------- // Lucent Technologies GIO/Unix // 4 Robbins Road, Westford, MA 01886 // 978-399-0481 / 888-480-6924 (Pager)
//-------------------------------------
--
Please see the official ORACLE-L FAQ: http://www.orafaq.com
--
Author: Gary Chambers
INET: gc22_at_lucent.com
Fat City Network Services -- (858) 538-5051 FAX: (858) 538-5051 San Diego, California -- Public Internet access / Mailing Liststo: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
Indien bovenstaand e-mailbericht niet aan u is gericht, verzoeken wij u vriendelijk doch dringend het e-mailbericht te retourneren aan de verzender en het origineel en eventuele kopieën te verwijderen en te vernietigen.
Ernst & Young hanteert bij de uitoefening van haar werkzaamheden algemene voorwaarden, waarin een beperking van aansprakelijkheid is opgenomen. De algemene voorwaarden worden u op verzoek kosteloos toegezonden.
If you are not the intended recipient of this communication please return the communication to the sender and delete and destroy all copies.
In carrying out its engagements, Ernst & Young applies general terms and conditions, which contain a clause that limits its liability. A copy of these terms and conditions is available on request free of charge.
--
Please see the official ORACLE-L FAQ: http://www.orafaq.com
--
Author: Jack van Zanen
INET: nlzanen1_at_EY.NL
Fat City Network Services -- (858) 538-5051 FAX: (858) 538-5051 San Diego, California -- Public Internet access / Mailing Liststo: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
Indien bovenstaand e-mailbericht niet aan u is gericht, verzoeken wij u vriendelijk doch dringend het e-mailbericht te retourneren aan de verzender en het origineel en eventuele kopieën te verwijderen en te vernietigen.
Ernst & Young hanteert bij de uitoefening van haar werkzaamheden algemene voorwaarden, waarin een beperking van aansprakelijkheid is opgenomen. De algemene voorwaarden worden u op verzoek kosteloos toegezonden.
If you are not the intended recipient of this communication please return the communication to the sender and delete and destroy all copies.
In carrying out its engagements, Ernst & Young applies general terms and conditions, which contain a clause that limits its liability. A copy of these terms and conditions is available on request free of charge.
--
Please see the official ORACLE-L FAQ: http://www.orafaq.com
--
Author: Jack van Zanen
INET: nlzanen1_at_EY.NL
Fat City Network Services -- (858) 538-5051 FAX: (858) 538-5051 San Diego, California -- Public Internet access / Mailing Liststo: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Thu Jul 18 2002 - 05:33:17 CDT
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message