DangerM0use Posted May 14, 2007 Share Posted May 14, 2007 I have a sales table and I need to make a query that takes the initial sale price and the last sale price. Now normally what I would do is a query that took the first sales_# and the last sales_# to do this. The problem is that the product may not be sold everytime - it may be withdrawn from sale and those details are kept in the same table. Is it possible to do a query that gets the first sales_# that has a sales_price or am I going to have to look again at how I have created my table? Cheers for your time and any help! Quote Link to comment https://forums.phpfreaks.com/topic/51291-ms-sql-query-question/ Share on other sites More sharing options...
o3d Posted May 30, 2007 Share Posted May 30, 2007 In postgres the logic could be as follows: create temp table tbltempa as select 1 as id,'aaa' as who, 12 as sale, cast('2005-05-05' as timestamp) as date union select 2, 'bbb',13, cast('2005-05-05' as timestamp) as date union select 3,'aaa', 14, cast('2005-05-07' as timestamp) as date union select 4,'bbb',15, cast('2005-05-08' as timestamp) as date union select 5,'aaa', 16, cast('2005-05-09' as timestamp) as date union select 6,'bbb',17, cast('2005-05-09' as timestamp) as date union select 7,'aaa', 18, cast('2005-05-10' as timestamp) as date union select 8,'bbb',19, cast('2005-05-10' as timestamp) as date select who, (select sale from tbltempa where date = min(a.date) and who = a.who) as min, (select sale from tbltempa where date = max(a.date) and who = a.who) as max from tbltempa as a group by a.who I worked quite a while ago in sql server, but the logic should be the same. Quote Link to comment https://forums.phpfreaks.com/topic/51291-ms-sql-query-question/#findComment-264535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.