How can list out the order details for each order...

which has the most recent purchase date

first of all, I'd like to get a subquery that
returns something like
Customer ID           Product Name          PurchaseDateTime          NetSales
1                                 Jade                            29-8-2013                         3000000
1                                 Gold Nuggets          29-8-2013                         3000000
1                                 Gold Bars                  28-8-2013                         3000000
  1. select c.CustomerID, p.ProductName, o.PurchaseDateTime, sum(Round(od.NetSales,2))
  2.           from customers As c
  3.                  INNER JOIN orders AS o ON (c.CustomerID = o.CustomerID)
  4.                  INNER JOIN order_details AS od ON (o.OrderID = od.OrderID)
  5.                  INNER JOIN products AS p ON (od.ProductID = p.ProductID)
  6.                group by c.CustomerID
複製代碼
Here I can't get individual Product Name
so I can feed into the next stage
  1. select c.CustomerID, p.ProductName, o.PurchaseDateTime, sum(Round(od.NetSales,2))
  2.           from customers As c
  3.                  INNER JOIN orders AS o ON (c.CustomerID = o.CustomerID)
  4.                  INNER JOIN order_details AS od ON (o.OrderID = od.OrderID)
  5.                  INNER JOIN products AS p ON (od.ProductID = p.ProductID)
  6.                  INNER JOIN (select c.CustomerID, max(o.PurchaseDateTime)
  7.                              AS lastVisit
  8.                              from customers AS c INNER JOIN
  9.                                  orders AS o ON (c.CustomerID = o.CustomerID)
  10.                             group by c.CustomerID) AS lastTimes
  11.                            ON o.PurchaseDateTime = lastTimes.lastVisit
  12.                      group by c.CustomerID
複製代碼
Customer ID  Product Name  Purchase Date NetSales
1                       Jade                     29-8-2013        3000000
1                      Gold Nuggets    29-8-2013        3000000

Any help please..
Thanks

simple but slow method:
  1. ....
  2. from    order
  3. where   trunc(PurchaseDateTime) in
  4.         (
  5.         select  max(trunc(PurchaseDateTime)) max_pu_dt
  6.         from    orders
  7.         )
複製代碼

TOP

where exists (select 1 from orders where PurchaseDateTime >=? and customerid = c.id)

TOP