Re: How do I write SQL
Posted in 1997
rich wrote: > > How do I write SQL to perform the following: > > I have a table that contains prices for five years. I need to select the last > price posted in each month. The record format is key,date,value -- the data in > any one month could be one record or up to twenty records. I need the output to > show the date and value. > > Thanks > > Rich Newell > richn@northv.com Does the solution have to be purely SQL? I would tend to do this in ACE, 4GL or ESQL/C. select in order of descending date. output the first record and every subsequent record in which the month and/or year were not equal to those of the previous record. If you need the output in reverse order either post-process the output or store it into a temporary table and select from that in ascending order. You don't state if the requirement is for single last record for the month or if you need the last record for each key value in the month. If it's the latter you'd need to take that into account. As they say in text books, the exercise is left to the reader. Ian