CASE statement page 3
Let's consider a few more examples.
Determine number of trips from Rostov to Moscow, and number of trips to other towns (not to Moscow).
Here we can use additional computed column for grouping by it.
Determine total number of trips from Rostov, and number of trips from Rostov to towns other than Moscow.
This problem requires to do grouping by two sets also, but now one set is a subset of another one. So exactly the same approach based on computed column is not fit here. In the previous task we had two non-intersecting sets to do grouping for each.
To solve this task we can calculate count over total set and use subquery to calculate the count over subset (second referense to a table), or we can use CASE statement in the conjunction with aggregate function to avoid second reading of a table. Let's check the estimation of optimizer for these two variants.
Use of subquery
Use of CASE with aggregate function
The result will be the same naturally:
You can compare the real run time of these queries on sufficient large amount of data.
The second variant can be rewritten in more short form with use of NULLIF function:
NULLIF function returns NULL, if its arguments are equal, or the first one otherwise.
The solution exploits the fact that the aggregate functions don't take into account NULL values which arise in argument of COUNT function when the landing town is 'Moscow'.