Počty a součty

Intermediate SQL Server

Ginger Grant

Instructor

Zkoumání celkových počtů

SELECT COUNT(*) FROM Incidents
+-----------------+
|(No column name) |
+-----------------+
|6452             | 
+-----------------+
Intermediate SQL Server

COUNT s DISTINCT

COUNT(DISTINCT COLUMN_NAME)
Intermediate SQL Server

COUNT s DISTINCT v T-SQL (I)

SELECT COUNT(DISTINCT Country) AS Countries
FROM Incidents
+----------------+
|Countries       |
+-----------------+
|3               |
+-----------------+
Intermediate SQL Server

COUNT s DISTINCT v T-SQL (II)

SELECT COUNT(DISTINCT Country) AS Countries,
COUNT(DISTINCT City) AS Cities 
FROM Incidents
+----------------+-------------+
|Countries       | Cities      |
+-----------------+-------------+
|3               | 3566        |
+-----------------+-------------+
Intermediate SQL Server

AGREGACE S COUNT

  • GROUP BY lze použít s COUNT() stejně jako s ostatními agregačními funkcemi, např. AVG(), MIN(), MAX()

  • Hodnoty lze seřadit pomocí příkazu ORDER BY

    • ASC vrátí nejmenší hodnoty jako první (výchozí)
    • DESC vrátí největší hodnoty jako první
Intermediate SQL Server

COUNT s GROUP BY v T-SQL

-- Count the rows, subtotaled by Country
SELECT COUNT(*) AS TotalRowsbyCountry, Country
FROM Incidents
GROUP BY Country 
+----------------------+-----------------+
|TotalRowsbyCountry   | Country         |
+----------------------+-----------------+
|5452                 |us               |
|750                  |NULL             |
|249                  |ca               |
|1                    |gb               |
+----------------------+-----------------+
Intermediate SQL Server

COUNT s GROUP BY a ORDER BY v T-SQL (I)

-- Count the rows, subtotaled by Country
SELECT COUNT(*) AS TotalRowsbyCountry, Country
FROM Incidents
GROUP BY Country 
ORDER BY Country ASC
+----------------------+-----------------+
|TotalRowsbyCountry   | Country         |
+----------------------+-----------------+
|750                  |NULL             |
|249                  |ca               |
|1                    |gb               |
|5452                 |us               |
+----------------------+-----------------+

Intermediate SQL Server

COUNT s GROUP BY a ORDER BY v T-SQL (II)

-- Count the rows, subtotaled by Country
SELECT COUNT(*) AS TotalRowsbyCountry, Country
FROM Incidents
GROUP BY Country 
ORDER BY Country DESC
+----------------------+-----------------+
|TotalRowsbyCountry   | Country         |
+----------------------+-----------------+
|5452                 |us               |
|1                    |gb               |
|249                  |ca               |
|750                  |NULL             |
+----------------------+-----------------+
Intermediate SQL Server

Součty sloupců pomocí SUM

  • SUM() vrátí číselný součet hodnot ve sloupci

  • Funguje stejně jako ostatní agregační funkce

  • V kombinaci s GROUP BY lze získat mezisoučty podle zadaných sloupců

Intermediate SQL Server

Sčítání hodnot sloupců v T-SQL

-- Calculate the values subtotaled by Country
SELECT SUM(DurationSeconds) AS TotalDuration, Country
FROM Incidents
GROUP BY Country
+----------+--------------------+
|Country   |TotalDuration       |
+----------+--------------------+
|us        |17024946.750001565  |
|null      |18859192.800000012  |
|ca        |200975              |
|gb        |120                 |
+----------+--------------------+

Intermediate SQL Server

Pojďme si procvičit!

Intermediate SQL Server

Preparing Video For Download...