SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sorting the price for last sale of an article
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

HarryCallaghan
Starting Member

5 Posts

Posted - 03/01/2013 :  02:57:30  Show Profile  Reply with Quote
Hi all.

Lets a say i have 2 tables:

Article: Info for every article we have sold: Code, description, reference.
Sales: All the sales we have done: Code of the sale, article sold, price, date etc.

I need to sort out what's the price for the last sale for each article, problem is that i can select max(date) and group by article but i don't know how to write the query in order i have something like article-descrip-ref-last date-sale of that date. ┬┐How to associate the max(date) with his corresponding price of sale?

Thanks a lot!

Edited by - HarryCallaghan on 03/01/2013 02:59:58

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 03/01/2013 :  03:04:13  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
something like this:

select a.description, a.reference, s.date, s.price
from Article a join Sales s on a.code = s.code
join
(
select code, max(date) from sales
group by code) t
on s.code = t.code and s.date = t.date


Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2013 :  03:30:11  Show Profile  Reply with Quote
quote:
Originally posted by harsh_athalye

something like this:

select a.description, a.reference, s.date, s.price
from Article a join Sales s on a.code = s.code
join
(
select code, max(date) as [date] from sales
group by code) t
on s.code = t.code and s.[date] = t.[date]


Harsh Athalye
http://www.letsgeek.net/



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2013 :  03:34:23  Show Profile  Reply with Quote
reading again i think it should be

select a.description, a.reference, s.date, s.price
from Article a join Sales s on a.code = s.article
join
(
select article, max(date) as [date] from sales
group by article) t
on s.article = t.article 
and s.[date] = t.[date]


as code in sales represent code of sale and not article

another way is this

select description, reference, [date], price
from
(
select a.description, a.reference, s.[date], s.price,row_Number() over (partition by a.description order by s.date desc) as seq
from Article a join Sales s on a.code = s.article
)t
where seq=1




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 03/01/2013 :  06:46:38  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
I liked your solution, visakh. Looks much elegant!

Harsh Athalye
http://www.letsgeek.net/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2013 :  06:58:13  Show Profile  Reply with Quote
quote:
Originally posted by harsh_athalye

I liked your solution, visakh. Looks much elegant!

Harsh Athalye
http://www.letsgeek.net/


thanks
happy to see you back after a long time

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

HarryCallaghan
Starting Member

5 Posts

Posted - 03/01/2013 :  06:58:57  Show Profile  Reply with Quote
Thanks a lot! i'll try that as soon as i can.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000