การรวมข้อมูล

SQL Server เบื้องต้น

John MacKintosh

Instructor

SUM - คอลัมน์เดียว

คำนวณผลรวมของค่าในคอลัมน์ด้วย SUM()

SELECT 
  SUM(affected_customers) AS total_affected 
FROM grid;
+----------------+
| total_affected | 
|----------------|
| 70143996       |  
+----------------+
SQL Server เบื้องต้น

SUM - สองคอลัมน์ขึ้นไป

SELECT 
  SUM(affected_customers) AS total_affected 
FROM grid;
SELECT 
  SUM (affected_customers) AS total_affected, 
  SUM (demand_loss_mw) AS total_loss 
FROM grid;
+----------------+------------+
| total_affected | total_loss | 
|----------------+------------|
| 70143996       | 177888     | 
+----------------+------------+
SQL Server เบื้องต้น

วิธีที่ผิด...

SELECT 
  SUM (affected_customers) AS total_affected, 
  (demand_loss_mw) AS total_loss 
FROM grid;
Msg 8120, Level 16, State 1, Line 6
Column 'grid_demand_loss_mw' is invalid in the select list because
it is not contained in either an aggregate function or the GROUP BY clause.
SQL Server เบื้องต้น

ใช้ alias

SELECT 
  SUM (affected_customers), 
  SUM (demand_loss_mw) 
FROM grid;
+------------------+------------------+
| (No column name) | (No column name) | 
|------------------+------------------|
| 70143996         | 177888           | 
+------------------+------------------+
SELECT 
  SUM (affected_customers) AS total_affected, 
  SUM (demand_loss_mw) AS total_loss 
FROM grid;
+----------------+------------+
| total_affected | total_loss | 
|----------------+------------|
| 70143996       | 177888     | 
+----------------+------------+
SQL Server เบื้องต้น

COUNT

SELECT 
  COUNT(affected_customers) AS count_affected 
FROM grid;
+----------------+
| count_affected | 
|----------------|
| 807            |  
+----------------+
SQL Server เบื้องต้น

COUNT Distinct

SELECT 
  COUNT(DISTINCT affected_customers) AS unique_count_affected 
FROM grid;
+-----------------------+
| unique_count_affected | 
|-----------------------|
| 280                   |   
+-----------------------+
SQL Server เบื้องต้น

MIN

SELECT 
  MIN(affected_customers) AS min_affected_customers 
FROM grid;
+------------------------+
| min_affected_customers | 
|------------------------|
| 0                      |   
+------------------------+
SELECT 
  MIN(affected_customers) AS min_affected_customers 
FROM grid 
WHERE affected_customers > 0;
+------------------------+
| min_affected_customers | 
|------------------------|
| 1                      |   
+------------------------+
SQL Server เบื้องต้น

MAX

SELECT 
  MAX(affected_customers) AS max_affected_customers 
FROM grid;
+------------------------+
| max_affected_customers | 
|------------------------|
| 4645572                |   
+------------------------+
SQL Server เบื้องต้น

ค่าเฉลี่ย

SELECT 
  AVG(affected_customers) AS avg_affected_customers 
FROM grid;
+------------------------+
| avg_affected_customers | 
|------------------------|
| 86919                  |   
+------------------------+
SQL Server เบื้องต้น

มาฝึกกันเถอะ!

SQL Server เบื้องต้น

Preparing Video For Download...