jakeisstoked
Technical User
Hi,
I need what I think should be a pretty simple SQL statement, but I just can't seem to find the solution.
It's only selecting data from 1 table..
Ok, so I have a table of prices associated with items, and the prices have a date (so it acts as kind of log as well), and there are also different types of prices; so I have a list of price types with an associated date, and the price.
e.g.
tbl_Prices
----------------
price_id | price | price_type | date
pretty simpe, no?
some example data:
10 | $100 | 'delivery' | 10/10/2005
11 | $500 | 'purchase' | 02/10/2005
12 | $20 | 'storage' | 08/10/2005
13 | $150 | 'delivery' | 12/10/2005
so basically this table will get pretty big, with any number of price changes, and also any number of 'types' of prices.
I need to select, from this table, all fields, from each price_type, but only the latest record for each type. I understand how distinct works, but it only applies to a single record, or a whole row (in which case all records are selected, because the are all different if distinct is applied to the row).
For example I wan't the result to be:
11 | $500 | 'purchase' | 02/10/2005
12 | $20 | 'storage' | 08/10/2005
13 | $150 | 'delivery' | 12/10/2005
Only selecting the latest record for each type of price.
This should be very simple, yes?
I can't work it through...
Any help? Bare in mind there can be any number of price types, all with a lot of updates...
This I can see how to solve, if I split the tables up into, say, tbl_latest_prices and tbl_price_log, but I don't want to, it's bad practice, and I have to control the logic with the forms/interface/whatever, and I would have redundant attributes.
Thanks for any ideas.
-Jake
I need what I think should be a pretty simple SQL statement, but I just can't seem to find the solution.
It's only selecting data from 1 table..
Ok, so I have a table of prices associated with items, and the prices have a date (so it acts as kind of log as well), and there are also different types of prices; so I have a list of price types with an associated date, and the price.
e.g.
tbl_Prices
----------------
price_id | price | price_type | date
pretty simpe, no?
some example data:
10 | $100 | 'delivery' | 10/10/2005
11 | $500 | 'purchase' | 02/10/2005
12 | $20 | 'storage' | 08/10/2005
13 | $150 | 'delivery' | 12/10/2005
so basically this table will get pretty big, with any number of price changes, and also any number of 'types' of prices.
I need to select, from this table, all fields, from each price_type, but only the latest record for each type. I understand how distinct works, but it only applies to a single record, or a whole row (in which case all records are selected, because the are all different if distinct is applied to the row).
For example I wan't the result to be:
11 | $500 | 'purchase' | 02/10/2005
12 | $20 | 'storage' | 08/10/2005
13 | $150 | 'delivery' | 12/10/2005
Only selecting the latest record for each type of price.
This should be very simple, yes?
I can't work it through...
Any help? Bare in mind there can be any number of price types, all with a lot of updates...
This I can see how to solve, if I split the tables up into, say, tbl_latest_prices and tbl_price_log, but I don't want to, it's bad practice, and I have to control the logic with the forms/interface/whatever, and I would have redundant attributes.
Thanks for any ideas.
-Jake