Groepen met gegevens maken

Introductie tot Oracle SQL

Hadrien Lacroix

Content Developer

Gegevens groeperen

Gegevens aggregeren

Introductie tot Oracle SQL

Groepsinformatie

| Composer        | Milliseconds |
|-----------------|--------------|
| Antonio Vivaldi | 199,086      |
| Pearl Jam       | 122,801      |
| Pearl Jam       | 65,593       |
| Jimmy Page      | 401,920      |
| Jimmy Page      | 386,063      |
| Jimmy Page      | 132,702      |
| Jimmy Page      | 189,675      |
| Jimmy Page      | 126,641      |
| Carlos Santana  | 318,432      |
| Carlos Santana  | 296,437      |
| Carlos Santana  | 882,834      |
| ...             | ...          |

Wat is de gemiddelde tracklengte per componist?

 

  • GROUP BY
    • verdeelt rijen in groepen
    • gebruik groepsfuncties voor samenvatting per groep
Introductie tot Oracle SQL

GROUP BY

SELECT Composer, AVG(Milliseconds)
FROM Track
GROUP BY Composer
| Composer        | AVG(Milliseconds) |
|-----------------|-------------------|
| Antonio Vivaldi | 199,086.0         |
| Pearl Jam       | 94,197.0          |
| Jimmy Page      | 474,888.3         |
| Carlos Santana  | 499,234.3         |
| ...             | ...               |
Introductie tot Oracle SQL

GROUP BY en WHERE

SELECT Composer, AVG(Milliseconds)
FROM Track
WHERE Genre = 1
GROUP BY Composer
| Composer        | AVG(Milliseconds) |
|-----------------|-------------------|
| Antonio Vivaldi | 199,086.0         |
| Pearl Jam       | 94,197.0          |
| Jimmy Page      | 474,888.3         |
| Carlos Santana  | 499,234.3         |
| ...             | ...               |
Introductie tot Oracle SQL

GROUP BY en ORDER BY

SELECT Composer, AVG(Milliseconds) AS Average
FROM Track
GROUP BY Composer
ORDER BY AVG(Milliseconds)
| Composer        | AVG(Milliseconds) |
|-----------------|-------------------|
| Pearl Jam       | 94,197.0          |
| Antonio Vivaldi | 199,086.0         |
| Jimmy Page      | 474,888.3         |
| Carlos Santana  | 499,234.3         |
| ...             | ...               |
Introductie tot Oracle SQL

GROUP BY en ORDER BY

SELECT Composer, AVG(Milliseconds) AS Average
FROM Track
GROUP BY Composer
ORDER BY 2
| Composer        | AVG(Milliseconds) |
|-----------------|-------------------|
| Pearl Jam       | 94,197.0          |
| Antonio Vivaldi | 199,086.0         |
| Jimmy Page      | 474,888.3         |
| Carlos Santana  | 499,234.3         |
| ...             | ...               |
Introductie tot Oracle SQL

GROUP BY en ORDER BY

SELECT Composer, AVG(Milliseconds) AS Average
FROM Track
GROUP BY Composer
ORDER BY Average
| Composer        | Average           |
|-----------------|-------------------|
| Pearl Jam       | 94,197.0          |
| Antonio Vivaldi | 199,086.0         |
| Jimmy Page      | 474,888.3         |
| Carlos Santana  | 499,234.3         |
| ...             | ...               |
Introductie tot Oracle SQL

Richtlijnen

Elke kolom of expressie in de SELECT-instructie die geen aggregatiefunctie is, moet in de GROUP BY-clausule staan

SELECT Composer, AVG(Milliseconds), UnitPrice
FROM Track
GROUP BY Composer
kolom "track.unitprice" moet in de GROUP BY-clausule staan of worden gebruikt in een aggregatiefunctie
REGEL 2: SELECT Composer, AVG(Milliseconds), UnitPrice
Introductie tot Oracle SQL

Richtlijnen

Elke kolom of expressie in de SELECT-lijst die geen aggregatiefunctie is, moet in de GROUP BY-clausule staan

SELECT Composer, AVG(Milliseconds), MAX(UnitPrice)
FROM Track
GROUP BY Composer
| Composer        | AVG(Milliseconds) | MAX(UnitPrice) |
|-----------------|-------------------|----------------|
| Antonio Vivaldi | 199,086.0         | 0.99           |
| Pearl Jam       | 94,197.0          | 0.99           |
| Jimmy Page      | 474,888.3         | 0.99           |
| Carlos Santana  | 499,234.3         | 0.99           |
| ...             | ...               | ...            |
Introductie tot Oracle SQL

Richtlijnen

Expressies die in GROUP BY staan, hoeven niet in de SELECT-instructie te staan

SELECT AVG(Milliseconds)
FROM Track
GROUP BY Composer
| AVG(Milliseconds) |
|-------------------|
| 199,086.0         |
| 94,197.0          |
| 474,888.3         |
| 499,234.3         |
| ...               |
Introductie tot Oracle SQL

Meerdere kolommen

SELECT Country, City, COUNT(CustomerId)
FROM Customer
GROUP BY Country, City
| Country   | City                | COUNT(CustomerId) |
|-----------|---------------------|-------------------|
| Argentina | Buenos Aires        | 1                 |
| Australia | Sidney              | 1                 |
| Austria   | Vienne              | 1                 |
| Belgium   | Brussels            | 1                 |
| Brazil    | Brasilia            | 1                 |
| Brazil    | São José dos Campos | 1                 |
| Brazil    | São Paulo           | 2                 |
| ...       | ...                 | ...               |
Introductie tot Oracle SQL

Laten we oefenen!

Introductie tot Oracle SQL

Preparing Video For Download...