Zadanie 5.

Pokaż pierwsze i ostatnie zamówienie dla klientów z Niemiec.

Baza: Northwind, Tabele: dbo.Orders, dbo.Customers, Kolumny: CustomerID, OrderID, Country, Wynik: 11 rows

s5

Rozwiązanie

SELECT CustomerID, MIN(OrderID) as FirstOrder, MAX(OrderID) as LastOrder
FROM dbo.Orders
GROUP BY CustomerID
HAVING CustomerID in (SELECT CustomerID FROM dbo.Customers WHERE Country = 'Germany')
ORDER BY CustomerID;

[collapse]