January 12, 2011 at 12:05 am
Table Orders
TeamId OrderDate Value
--------------------------------
A1 2010-11-05 1200.00
A2 2010-11-05 1000.00
A1 2010-11-07 700.00
A1 2010-11-10 5000.00
A2 2010-11-09 1100.00
I need to write a wuery which can give the result:
TeamId OrderDate Value
--------------------------------
A1 2010-11-10 5000.00
A2 2010-11-09 1100.00
(I mean TeamId wise the latest order)
January 12, 2011 at 12:16 am
I need to write a wuery which can give the result:
TeamId OrderDate Value
--------------------------------
A1 2010-11-10 5000.00
A2 2010-11-09 1100.00
(I mean TeamId wise the latest order)
SELECT DISTINCT TeamId, OrderDate, Value from yourtable
ORDER by OrderDate DESC
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle
January 12, 2011 at 12:23 am
No No. I just need one record per Team Id. The one with latest Orderdate. Consider there are lots of Teams with hundrds of OrderDates.
January 12, 2011 at 1:01 am
sql_butterfly (1/12/2011)
No No. I just need one record per Team Id. The one with latest Orderdate. Consider there are lots of Teams with hundrds of OrderDates.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle
January 12, 2011 at 1:22 am
Your message doesnot shows anything....
January 12, 2011 at 1:26 am
January 12, 2011 at 3:04 am
please post insert scripts in future.
try this
select a.TeamID, a.OD OrderDate, b.value
from
(select TeamID, max(OrderDate) OD
from yourtable
group by TeamID) a
join yourtable b
on a.TeamID=b.TeamID
and a.OrderDate=b.OD
January 12, 2011 at 3:25 am
Great. Thanks, that helped.
January 12, 2011 at 3:27 am
Pleasure,
If your table is rather large, I would insert the subquery in a temp table.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply