![]() | |
#11
| |||
| |||
|
|
Tom Anderson wrote on 03.05.2010 15:14: Firstly, how can i ask for: the latest temperature from each station SELECT station_number, temperature FROM weather_reports wr WHERE wr.report_date = (SELECT max(report_date) FROM weather_reports w2 WHERE w2.station_number = wr.station_number) |
|
and some operator which takes the first row in each sorted group. I don't know what that last operator is, and i don't know how to put them all together. This is called windowing functions, and the corresponding function would be first_value() Sorry to ask such a basic question; if there's any fine manual i could read online that would let me work this out myself, please don't hesitate to tell me to read it. http://www.postgresql.org/docs/curre...al-window.html Windowing functions are defined in the SQL standard and supported by all major DBMS The above tutorial will also work for other DBMS that comply with the standard |
#12
| |||
| |||
|
|
On 03.05.2010 15:14, Tom Anderson wrote: If i was doing this in imperative code, the former would look like: for each station: sort all the reports by date take the latest But i don't know how to transpose this to SQL. I'm guessing that involves some combination of GROUP BY station_number, to give the aspect of working over all the stations, ORDER BY report_date, to give the aspect of looking at the data in terms of time, and some operator which takes the first row in each sorted group. I don't know what that last operator is, and i don't know how to put them all together. You need GROUP BY but you do not need ORDER BY - rather you need MAX. |
#13
| |||
| |||
|
|
You would do well to read C.J. Date's “Introduction to Database Systems” URL:http://en.wikipedia.org/wiki/Special:BookSources/0321197844>. |
#14
| |||
| |||
|
|
On 05/04/2010 04:54 AM, Ben Finney wrote: You would do well to read C.J. Date's ?Introduction to Database Systems? URL:http://en.wikipedia.org/wiki/Special:BookSources/0321197844>. I also dare to suggest, after that, the third edition of "Joe Celko's SQL for smarties" - I found it gentler towards the naive readers, and less 'taliban' (more practical) than other books from him or Date. |
|
As for Date's, even in the latest "SQL and relational theory", with his utter denial of NULL and the like, I think he's teaching me to program in a SQL variant that does not exists in the wild. The day Postgres supports Tutorial-D, I'll be the first to have a picture of him in my wallet. |
#15
| |||
| |||
|
|
On Mon, 3 May 2010, Robert Klemme wrote: On 03.05.2010 15:14, Tom Anderson wrote: If i was doing this in imperative code, the former would look like: for each station: sort all the reports by date take the latest But i don't know how to transpose this to SQL. I'm guessing that involves some combination of GROUP BY station_number, to give the aspect of working over all the stations, ORDER BY report_date, to give the aspect of looking at the data in terms of time, and some operator which takes the first row in each sorted group. I don't know what that last operator is, and i don't know how to put them all together. You need GROUP BY but you do not need ORDER BY - rather you need MAX. That is exactly the key thing i was missing. It's part of a very different way of thinking of things to what i'm used to. |
![]() |
| Thread Tools | |
| Display Modes | |
| |