Re: Very Tricky SELECT statement

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

From: Eric Sabine (mopar41_at____ho_y_tmail.ScPoAmM)
Date: 04/05/04


Date: Mon, 5 Apr 2004 16:11:19 -0400

Here's an example of how you would do it with a correlated subquery. It can
be adapted to your table schema.

use northwind
go

select a.*,
 (select top 1 productid
 from [Order Details] b
 where a.orderid = b.orderid
 order by b.productid desc) as productId
from orders a

hth
Eric

"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

  • 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: Multiple Many to Many
    ... INT NOT NULL PRIMARY KEY, ... CREATE TABLE [Order Details] ... ProdID INT NOT NULL FOREIGN KEY REFERENCES ProductsON DELETE CASCADE ...
    (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)
  • 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)
  • 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)