|
Complex SELECTs
By Kalman Toth, M.Phil., M.Phil., MCDBA
May 16, 2005
The beauty of SQL language is its uncanny ability to manipulate
sets of data. Exactly where other programming languages fall short due to lack of built-in looping. When I write "select * from Alpha" in SQL, I am telling the database engine: loop through the entire Alpha table and return the content of each row.
In the following example instead of joining 2 tables, we are joining
2 SELECT result sets (subselects, derived tables). We added the "Seq" column to the
select list and populated it with the value 1. We are using it
to generate an ordinal (sequence number) for each CategoryID, SupplierID combination.
-- SQL sequence number (LineNo) generation within groups
USE NorthWind;
SELECT b.CategoryID,
b.SupplierID,
b.ProductName,
[LineNo] = sum(b.seq)
FROM (SELECT CategoryID,
SupplierID,
ProductName,
1 AS seq
FROM dbo.Products) a
INNER JOIN
(SELECT CategoryID,
SupplierID,
ProductName,
1 AS seq
FROM dbo.Products) b
ON a.SupplierID = b.SupplierID
AND a.CategoryID = b.CategoryID
WHERE b.ProductName >= a.ProductName
GROUP BY b.CategoryID,
b.SupplierID,
b.ProductName
GO
The partial result is below. The RowSeqNo breaks and starts over
whenever the CategoryID and/or SupplierID changes.
| CategoryID |
SupplierID |
ProductName |
LineNo |
| 1 |
1 |
Chai |
1 |
| 1 |
1 |
Chang |
2 |
| 1 |
7 |
Outback Lager |
1 |
| 1 |
10 |
Guaraná Fantástica |
1 |
| 1 |
12 |
Rhönbräu Klosterbier |
1 |
| 1 |
16 |
Laughing Lumberjack Lager |
1 |
| 1 |
16 |
Sasquatch Ale |
2 |
| 1 |
16 |
Steeleye Stout |
3 |
| 1 |
18 |
Chartreuse verte |
1 |
| 1 |
18 |
Côte de Blaye |
2 |
| 1 |
20 |
Ipoh Coffee |
1 |
| 1 |
23 |
Lakkalikööri |
1 |
| 2 |
1 |
Aniseed Syrup |
1 |
| 2 |
2 |
Chef Anton's Cajun Seasoning |
1 |
| 2 |
2 |
Chef Anton's Gumbo Mix |
2 |
| 2 |
2 |
Louisiana Fiery Hot Pepper Sauce |
3 |
| 2 |
2 |
Louisiana Hot Spiced Okra |
4 |
| 2 |
3 |
Grandma's Boysenberry Spread |
1 |
| 2 |
3 |
Northwoods Cranberry Sauce |
2 |
| 2 |
6 |
Genen Shouyu |
1 |
| 2 |
7 |
Vegie-spread |
1 |
| 2 |
12 |
Original Frankfurter grüne Soße |
1 |
| 2 |
20 |
Gula Malacca |
1 |
| 2 |
29 |
Sirop d'érable |
1 |
| 3 |
7 |
Pavlova |
1 |
| 3 |
8 |
Scottish Longbreads |
1 |
| 3 |
8 |
Sir Rodney's Marmalade |
2 |
| 3 |
8 |
Sir Rodney's Scones |
3 |
| 3 |
8 |
Teatime Chocolate Biscuits |
4 |
| 3 |
11 |
Gumbär Gummibärchen |
1 |
| 3 |
11 |
NuNuCa Nuß-Nougat-Creme |
2 |
| 3 |
11 |
Schoggi Schokolade |
3 |
| 3 |
22 |
Chocolade |
1 |
| 3 |
22 |
Zaanse koeken |
2 |
| 3 |
23 |
Maxilaku |
1 |
| 3 |
23 |
Valkoinen suklaa |
2 |
| 3 |
29 |
Tarte au sucre |
1 |
| 4 |
5 |
Queso Cabrales |
1 |
| 4 |
5 |
Queso Manchego La Pastora |
2 |
| 4 |
14 |
Gorgonzola Telino |
1 |
| 4 |
14 |
Mascarpone Fabioli |
2 |
| 4 |
14 |
Mozzarella di Giovanni |
3 |
| 4 |
15 |
Flotemysost |
1 |
| 4 |
15 |
Geitost |
2 |
| 4 |
15 |
Gudbrandsdalsost |
3 |
| 4 |
28 |
Camembert Pierrot |
1 |
| 4 |
28 |
Raclette Courdavault |
2 |
| 5 |
9 |
Gustaf's Knäckebröd |
1 |
| 5 |
9 |
Tunnbröd |
2 |
| 5 |
12 |
Wimmers gute Semmelknödel |
1 |
| 5 |
20 |
Singaporean Hokkien Fried Mee |
1 |
| 5 |
24 |
Filo Mix |
1 |
| 5 |
26 |
Gnocchi di nonna Alice |
1 |
| 5 |
26 |
Ravioli Angelo |
2 |
| 6 |
4 |
Mishi Kobe Niku |
1 |
| 6 |
7 |
Alice Mutton |
1 |
| 6 |
12 |
Thüringer Rostbratwurst |
1 |
| 6 |
24 |
Perth Pasties |
1 |
| 6 |
25 |
Pâté chinois |
1 |
| 6 |
25 |
Tourtière |
2 |
| 7 |
3 |
Uncle Bob's Organic Dried Pears |
1 |
| 7 |
4 |
Longlife Tofu |
1 |
| 7 |
6 |
Tofu |
1 |
| 7 |
12 |
Rössle Sauerkraut |
1 |
| 7 |
24 |
Manjimup Dried Apples |
1 |
| 8 |
4 |
Ikura |
1 |
| 8 |
6 |
Konbu |
1 |
| 8 |
7 |
Carnarvon Tigers |
1 |
| 8 |
13 |
Nord-Ost Matjeshering |
1 |
| 8 |
17 |
Gravad lax |
1 |
| 8 |
17 |
Inlagd Sill |
2 |
| 8 |
17 |
Röd Kaviar |
3 |
| 8 |
19 |
Boston Crab Meat |
1 |
| 8 |
19 |
Jack's New England Clam Chowder |
2 |
| 8 |
21 |
Rogede sild |
1 |
| 8 |
21 |
Spegesild |
2 |
| 8 |
27 |
Escargots de Bourgogne |
1 |
|