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
 The Last Order of all the product
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Flaterik
Starting Member

Italy
26 Posts

Posted - 05/21/2012 :  09:26:51  Show Profile  Reply with Quote
Hi people i have the table DORIG in my db.
In this table i have all the row of the invoice.

The table have this structure

id_Dorig, Cd_Cf, datadoc, cd_ar and more other field that are not usefull.

What i whant is data of the last document for all the article.

The data that i want extract is

Cd_ar , datadoc , cd_cf

If i use select MAX(datadoc) , cd_ar
from dorig group by cd_ar

I have the cd_ar and the data but i can't extract the cd_cf. If i insert it in the query, duplicate result.

Thank you


************************************************
the world is strange but people are crazy

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/21/2012 :  09:35:32  Show Profile  Reply with Quote

select *
from
(
   select *, rn = row_number() over (partition by cd_ar order by datadoc desc)
   from   dorig
) d
where d.rn = 1



KH
Time is always against us

Go to Top of Page

Flaterik
Starting Member

Italy
26 Posts

Posted - 05/21/2012 :  09:40:24  Show Profile  Reply with Quote
God bless you!

************************************************
the world is strange but people are crazy
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/21/2012 :  09:43:46  Show Profile  Reply with Quote
welcome


KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47081 Posts

Posted - 05/21/2012 :  16:19:24  Show Profile  Reply with Quote
quote:
Originally posted by Flaterik

Hi people i have the table DORIG in my db.
In this table i have all the row of the invoice.

The table have this structure

id_Dorig, Cd_Cf, datadoc, cd_ar and more other field that are not usefull.

What i whant is data of the last document for all the article.

The data that i want extract is

Cd_ar , datadoc , cd_cf

If i use select MAX(datadoc) , cd_ar
from dorig group by cd_ar

I have the cd_ar and the data but i can't extract the cd_cf. If i insert it in the query, duplicate result.

Thank you


************************************************
the world is strange but people are crazy


you were close. to just get other data you just needed to modify it as

SELECT t.*
FROM dorig t
INNER JOIN (select MAX(datadoc) AS MaxDate, cd_ar 
from dorig group by cd_ar
)t1
ON t1.cd_ar = t.cd_ar
AND t1.MaxDate = t.datadoc


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

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.08 seconds. Powered By: Snitz Forums 2000