Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause

Many times we might need to use aggregated values in our Where Clause, as a way of limiting the result set of aggregated values. You would think it would be as easy as adding a Where Clause in our query statement, however we will see in this episode,…


This content originally appeared on DEV Community and was authored by Goran Kortjie

goran-says-hi

Many times we might need to use aggregated values in our Where Clause, as a way of limiting the result set of aggregated values. You would think it would be as easy as adding a Where Clause in our query statement, however we will see in this episode, this is not the case.

skid-12

By using the previous episode as an example, you can check that out on the link below to get up to speed.

It's a quick read so once you're done head on back here!

skid-cover-4

.

We might want to select all those customers whose order count is less than two times or more than two, three, four, five etc...

skid-11

The Count is in this example the aggregated value. However a Where Clause does not take in an aggregated value. In this episode we are going to discuss how to use aggregated values to select our records.

Lets move into SMSS and see this in practice.

In this example we will be using the Sales.SalesOrderHeader table. As we know this table stores the information for every order the customer has placed. There is essentially one record for each sales order, then there are CustomerID's that stores information about the customer who placed this order.

skid-11

What we are trying to do here is to find the CustomerID Where the sales count is more than X / less than X

First we will start by using a SELECT statement and select the CustomerID and the SalesOrderID.

discussion-1

What we need now is the Count of Sales Orders. Therefore we wrap the salesOrderId in the Count function. Notice we are grouping by the Customer level, meaning for each customer give us the count of their salesOrderId's.

skid-7

Finally we need to tell SQL where to group it by and in this case we group it by the CustomerID.

discussion-2

To make it easier to understand we could add an Alias to the Count function that wraps around salesOrderId.

skid-7

In order to only select the customers whose sales order Count is less than two, in this scenario, normally we would use the Where clause. However if we try this we will see SSMS throws an error.

discussion-3

Instead of Where, we replace it with Having. Also notice we cannot use an Alias when we use Having, we must use the complete Count function query string containing the salesOrderId.

discussion-4

Voila!

skid-12

Now we can see all the customers whose sales order Count is less than two. We could even change the operator to less than equal to two, which in this case will return customers whose sale orders are two and less than two in the result set.

Awesome right!🥳

sql

Keep in mind, for any of the aggregated functions, such as MIN, MAX, AVG or SUM, you will have to use HAVING if you want to restrict your result set, by those aggregated values.

skid-cover-4

I hope you enjoyed this episode, happy learning...

meditation


This content originally appeared on DEV Community and was authored by Goran Kortjie


Print Share Comment Cite Upload Translate Updates
APA

Goran Kortjie | Sciencx (2021-11-10T18:48:00+00:00) Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause. Retrieved from https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/

MLA
" » Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause." Goran Kortjie | Sciencx - Wednesday November 10, 2021, https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/
HARVARD
Goran Kortjie | Sciencx Wednesday November 10, 2021 » Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause., viewed ,<https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/>
VANCOUVER
Goran Kortjie | Sciencx - » Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/
CHICAGO
" » Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause." Goran Kortjie | Sciencx - Accessed . https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/
IEEE
" » Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause." Goran Kortjie | Sciencx [Online]. Available: https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/. [Accessed: ]
rf:citation
» Learn SQL: Microsoft SQL Server – Episode 9: Aggregation in Where Clause | Goran Kortjie | Sciencx | https://www.scien.cx/2021/11/10/learn-sql-microsoft-sql-server-episode-9-aggregation-in-where-clause/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.