Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sql max of one field
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01412746
Message ID:
01412751
Vues:
51
>I have a table as follows with 2 columns :
>CUS Date (Date field)
>A 10 May
>A 12 May
>A 14 May
>B 6 Jan
>B 9 JUN
>B 20 DEC
>
>Is there a Sql select which will select out all customers but with most recent date only
>So I would require:
>A 14 May
>B 20 Dec only
>
>Tia
>Gerard

Ok, too bad this blog is not published yet :(((((((

Anyway, I'll post two quick solutions from it
SELECT
   C.AccountNumber,
   O.OrderDate,
   O.SubTotal
FROM
   Sales.Customer C
   INNER JOIN Sales.SalesOrderHeader O ON C.CustomerID = O.CustomerID
WHERE
   O.OrderDate = (
      SELECT MAX(OrderDate)
      FROM SalesOrderHeader O2
      WHERE O2.CustomerID = O.CustomerID
another one
SELECT
   C.AccountNumber,
   O.OrderDate,
   O.SubTotal
FROM
   Sales.Customer C
   INNER JOIN Sales.SalesOrderHeader O ON C.CustomerID = O.CustomerID
   INNER JOIN (
      SELECT CustomerID, LastOrderDate = MAX(OrderDate)
      FROM Sales.SalesOrderHeader
      GROUP BY CustomerID
   ) O2
   ON C.CustomerID = O2.CustomerID AND C.LastOrderDate = O.OrderDate
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform