SUM
It is used to calculate the total amount of a column.
Example:
1 2 3
SELECT SUM(affected_customers) AS total_affected FROM grid;
Returns the total of all the
affected_customers
1 2 3 4
SELECT SUM(affected_customers) AS total_affected, SUM(demand_loss_mw) AS total_loss FROM grid;
Returns the total of all the
affected_customers
amddemand_loss_mw
columns.
COUNT
It returns the total count of the records
COUNT DISTINCT
It returns the total count of the distinct records
MIN
It returns the minimum value of a column
MAX
It returns the maximum value of a column
AVG
It returns the average value of a column
LEN
It returns the length of a string
LEFT
It returns the specified no of characters from the string from the left side.
RIGHT
It returns the specified no of characters from the string from the right side.
CHARINDEX
The CHARINDEX() function searches for a substring from the left-side in a string, and returns the position. If the substring is not found, this function returns 0.
SUBSTRING
It returns the part of the string from the specified starting location to the specified end location.
Syntax: REPLACE(string, start, end)
REPLACE
IT replaces all occurrences of a substring within a string, with a new substring.
Syntax: REPLACE(string, old_string, new_string)
GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country”. The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
Example:
1 2 3 4 5
SELECT SUM(demand_loss_mw) AS lost_demand, description FROM grid GROUP BY description;
The above SQL statement groups the records by description and for each group calculates the sum of
demand_loss_mw
.1 2 3 4 5 6 7 8
SELECT SUM(demand_loss_mw) AS lost_demand, description FROM grid WHERE description LIKE '%storm' AND demand_loss_mw IS NOT NULL GROUP BY description;
The above SQL statement groups the records by description and filters the records with the conditions specified in WHERE clause and then aggregates the
demand_loss_mw
field using the SUM function from the result queires.
HAVING
The
HAVING
clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. It can be used when filtering with groups. We normally useWHERE
to filter data but what if we want to sum values based on groups and then filter on those groups? In such cases, we useHAVING
.Example:
1 2 3 4 5 6 7 8 9
SELECT SUM(demand_loss_mw) AS lost_demand, description FROM grid WHERE description LIKE '%storm' AND demand_loss_mw IS NOT NULL GROUP BY description HAVING SUM(demand_loss_mw) > 1000;
The above SQL statement groups the records by description and filters the records with the conditions specified in WHERE clause and then aggregates using the SUM function from the result queires. Then the results after applying aggregating functions are filterd using the conditions specified in the
Having
clause.
GROUP BY
splits the data up into combination sof one or more values.WHERE
filters on row valuesHAVING
appears after theGROUP BY
claues and filters on groups or aggregates
Aggregating Data
This post is licensed under CC BY 4.0 by the author.
Contents