Chapter2 - SQL Server
Chapter2 - SQL Server
I N T R O D U C T I O N TO S Q L S E R V E R
John MacKintosh
Instructor
SUM - single column
Calculate the total amount of a column value with SUM()
SELECT
SUM(affected_customers) AS total_affected
FROM grid;
+----------------+
| total_affected |
|----------------|
| 70143996 |
+----------------+
SELECT
SUM (affected_customers) AS total_affected,
SUM (demand_loss_mw) AS total_loss
FROM grid;
+----------------+------------+
| total_affected | total_loss |
|----------------+------------|
| 70143996 | 177888 |
+----------------+------------+
+------------------+------------------+ +----------------+------------+
| (No column name) | (No column name) | | total_affected | total_loss |
|------------------+------------------| |----------------+------------|
| 70143996 | 177888 | | 70143996 | 177888 |
+------------------+------------------+ +----------------+------------+
+----------------+
| count_affected |
|----------------|
| 807 |
+----------------+
+-----------------------+
| unique_count_affected |
|-----------------------|
| 280 |
+-----------------------+
+------------------------+
| min_affected_customers | +------------------------+
|------------------------| | min_affected_customers |
| 0 | |------------------------|
+------------------------+ | 1 |
+------------------------+
+------------------------+
| max_affected_customers |
|------------------------|
| 4645572 |
+------------------------+
+------------------------+
| avg_affected_customers |
|------------------------|
| 86919 |
+------------------------+
John MacKintosh
Instructor
SELECT
description,
LEN(description) AS description_length
FROM grid;
+----------------+-----------------------------------+
| description | description_length |
|-------------------------------+--------------------|
| Severe Weather Thunderstorms | 29 |
| Severe Weather Thunderstorms | 29 |
| Severe Weather Thunderstorms | 29 |
| Fuel Supply Emergency Coal | 27 |
| Physical Attack Vandalism | 26 |
+----------------+-----------------------------------+
+-------------------------------+-----------------------+
| description | first_20_left |
|-------------------------------+-----------------------|
| Severe Weather Thunderstorms | Severe Weather Thun |
| Severe Weather Thunderstorms | Severe Weather Thun |
| Severe Weather Thunderstorms | Severe Weather Thun |
| Fuel Supply Emergency Coal | Fuel Supply Emergenc |
| Physical Attack Vandalism | Physical Attack Van |
+-------------------------------+-----------------------+
+-------------------------------+----------------------+
| description | last_20 |
|-------------------------------+----------------------|
| Severe Weather Thunderstorms | ather Thunderstorms |
| Severe Weather Thunderstorms | ather Thunderstorms |
| Severe Weather Thunderstorms | ather Thunderstorms |
| Fuel Supply Emergency Coal | pply Emergency Coal |
| Physical Attack Vandalism | al Attack Vandalism |
+-------------------------------+----------------------+
+---------------+-------------------------------------+
| char_location | url |
|---------------+-------------------------------------|
| 34 | datacamp.com/courses/introduction_ |
| 34 | datacamp.com/courses/intermediate_ |
| 29 | datacamp.com/courses/writing_ |
| 29 | datacamp.com/courses/joining_ |
| 27 | datacamp.com/courses/intro_ |
+---------------+-------------------------------------+
+------------------+----------------------------------+
| target_section | url |
+------------------+----------------------------------+
| www.datacamp.com |https//www.datacamp.com/courses |
+------------------+----------------------------------+
+-------------------------------------+
| replace_with_hyphen |
+-------------------------------------|
| datacamp.com/courses/introduction- |
| datacamp.com/courses/intermediate- |
| datacamp.com/courses/writing- |
| datacamp.com/courses/joining- |
| datacamp.com/courses/intro- |
+-------------------------------------+
John MacKintosh
Instructor
A simple SELECT
SELECT
SUM(demand_loss_mw) AS lost_demand
FROM grid;
+-------------+
| lost_demand |
+-------------+
| 177888 |
+-------------+
SELECT
SUM(demand_loss_mw) AS lost_demand,
description
FROM grid;
+----------- -+-------------------------------------------------------+
| lost_demand | description |
+-------------+-------------------------------------------------------+
| NULL | Actual Physical Attack |
| NULL | Cold Weather Event |
| NULL | Cyber Event with Potential to Cause Impact |
| 40 | Distribution Interruption |
| 2 | Distribution System Interruption |
| NULL | Earthquake |
| NULL | Electrical Fault at Generator |
| 338 | Electrical System Islanding |
| 24514 | Electrical System Separation Islanding |
| 15 | Electrical System Separation Islanding Severe Weather |
+-------------+-------------------------------------------------------+
+----------- -+--------------------------------------------+
| lost_demand | description |
|-------------+--------------------------------------------|
| 996 | Ice Storm |
| 420 | Load Shed Severe Weather Lightning Storm |
| 332 | Major Storm |
| 3 | Severe Weather Thunderstorm |
| 413 | Severe Weather Wind Storm |
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
+----------- -+--------------------------------------------+
| lost_demand | description |
+-------------+--------------------------------------------+
| 996 | Ice Storm |
| 420 | Load Shed Severe Weather Lightning Storm |
| 332 | Major Storm |
| 3 | Severe Weather Thunderstorm |
| 413 | Severe Weather Wind Storm |
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
+----------- -+--------------------------------------------+
| lost_demand | description |
|-------------+--------------------------------------------|
| 4171 | Severe Weather Winter Storm |
| 1352 | Winter Storm |
+-------------+--------------------------------------------+
HAVING appears after the GROUP BY clause and lters on groups or aggregates