Hitungan dan Total

SQL Server Menengah

Ginger Grant

Instructor

Meninjau Total dengan COUNT

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

COUNT dengan DISTINCT

COUNT(DISTINCT COLUMN_NAME)
SQL Server Menengah

COUNT dengan DISTINCT di T-SQL (I)

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

COUNT dengan DISTINCT di T-SQL (II)

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

AGREGASI COUNT

  • GROUP BY dapat dipakai dengan COUNT() seperti fungsi agregasi lain, mis. AVG(), MIN(), MAX()

  • Gunakan perintah ORDER BY untuk mengurutkan nilai

    • ASC menampilkan nilai terkecil dulu (bawaan)
    • DESC menampilkan nilai terbesar dulu
SQL Server Menengah

COUNT dengan GROUP BY di T-SQL

-- Hitung baris, subtotal per Country
SELECT COUNT(*) AS TotalRowsbyCountry, Country
FROM Incidents
GROUP BY Country 
+----------------------+-----------------+
|TotalRowsbyCountry   | Country         |
+----------------------+-----------------+
|5452                 |us               |
|750                  |NULL             |
|249                  |ca               |
|1                    |gb               |
+----------------------+-----------------+
SQL Server Menengah

COUNT dengan GROUP BY dan ORDER BY di T-SQL (I)

-- Hitung baris, subtotal per 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               |
+----------------------+-----------------+

SQL Server Menengah

COUNT dengan GROUP BY dan ORDER BY di T-SQL (II)

-- Hitung baris, subtotal per 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             |
+----------------------+-----------------+
SQL Server Menengah

Total kolom dengan SUM

  • SUM() menjumlahkan nilai numerik dalam kolom

  • Polanya sama seperti agregasi lain

  • Gabungkan dengan GROUP BY untuk subtotal berdasarkan kolom yang ditentukan

SQL Server Menengah

Menjumlahkan nilai kolom di T-SQL

-- Hitung nilai subtotal per Country
SELECT SUM(DurationSeconds) AS TotalDuration, Country
FROM Incidents
GROUP BY Country
+----------+--------------------+
|Country   |TotalDuration       |
+----------+--------------------+
|us        |17024946.750001565  |
|null      |18859192.800000012  |
|ca        |200975              |
|gb        |120                 |
+----------+--------------------+

SQL Server Menengah

Ayo berlatih!

SQL Server Menengah

Preparing Video For Download...