Re: Very Tricky SELECT statement

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Trey Walpole (treyNOpole_at_SPcomcastAM.net)
Date: 04/05/04


Date: Mon, 5 Apr 2004 15:09:41 -0500

use a correlated subquery to find the most recent date for the given order,
e.g.

select <orders columns>, <order detail columns>
from orders
join orderdetail on orders.OrderID=orderdetail.OrderID
where orderdetail.orderdate=
(select max(orderdate) from orderdetail where OrderID=orders.OrderID)

"Stewart Saathoff" <stewarts@complete-networking.com> wrote in message
news:eqnpni0GEHA.1264@TK2MSFTNGP10.phx.gbl...
> Hey Everyone,
>
> I have a very tricky select statement that I need help with.
>
> I have a table that contains orders and order details.
>
> I want to retrieve the last detail record from every unique order that was
> placed. The last record is known by the exact date and time that it was
> ordered. The order details table is similar to this:
>
> orderdetid PK int
> orderid FK int
> Item String
> orderdate date/time
>
> I know how to retrieve the last records in a database using the TOP
command.
> but I am not sure how to retrieve every top record from say 5000 orders...
>
>



Relevant Pages

  • Re: Very Tricky SELECT statement
    ... > from orderdetail where OrderID=orders.OrderID) ... >> I have a table that contains orders and order details. ... >> orderdetid PK int ... >> I know how to retrieve the last records in a database using the TOP ...
    (microsoft.public.sqlserver.programming)
  • Very Tricky SELECT statement
    ... I have a table that contains orders and order details. ... orderdetid PK int ... I know how to retrieve the last records in a database using the TOP command. ...
    (microsoft.public.sqlserver.programming)
  • Re: Very Tricky SELECT statement
    ... > I have a table that contains orders and order details. ... > orderdetid PK int ... > I know how to retrieve the last records in a database using the TOP ...
    (microsoft.public.sqlserver.programming)
  • Re: Test
    ... Statement for OrderDetail: ...
    (microsoft.public.sqlserver.programming)
  • Re: Very Tricky SELECT statement
    ... > I have a table that contains orders and order details. ... > orderdetid PK int ... > I know how to retrieve the last records in a database using the TOP command. ...
    (microsoft.public.sqlserver.programming)