Jump to content

MS SQL query question


Recommended Posts

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!

Link to comment
Share on other sites

  • 3 weeks later...

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.