T-SQL: Subquery – SELECT

Please see my other Database Development articles.

Performing multiple, concurrent queries

Often when needing a resultset, there’s a need to perform multiple queries to derive the desired data.
In this example, we have multiple records of people, from which we need an aggregated report.

1-t-sql-subquery-select

In order to accomplish this goal, I must perform two separate queries on the same data.
First, I list each person’s age, the sum of all peoples’ ages, then their age as a percentage compared against the total of all ages.

2-t-sql-subquery-select

The final data illustrates the intended aggregation.

3-t-sql-subquery-select

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s