Query Help

  • 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)

  • 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

  • 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.

  • 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

  • Your message doesnot shows anything....

  • Try this article

    http://www.sqlservercentral.com/articles/T-SQL/71571/



    Clear Sky SQL
    My Blog[/url]

  • 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

  • Great. Thanks, that helped.

  • 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