Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sorting the price for last sale of an article

Author  Topic 

HarryCallaghan
Starting Member

5 Posts

Posted - 2013-03-01 : 02:57:30
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!

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2013-03-01 : 03:04:13
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

52326 Posts

Posted - 2013-03-01 : 03:30:11
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

52326 Posts

Posted - 2013-03-01 : 03:34:23
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
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2013-03-01 : 06:46:38
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

52326 Posts

Posted - 2013-03-01 : 06:58:13
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 - 2013-03-01 : 06:58:57
Thanks a lot! i'll try that as soon as i can.
Go to Top of Page
   

- Advertisement -