T-SQL: Pivot

Please see my other Database Development articles.

Dynamically restructuring report data

Often when creating reports, data is received in varying formats, some of which make it difficult to create a visually compelling format. One example of such data is when the structure itself is embedded within the data.

As you see below, the initial record set begins with a company’s data. The native structure of the data rests within the “dept_type” column with values of accounting, sales, and it.

1-t-sql-pivot

2-t-sql-pivot

After executing a simple SELECT statement making use of the PIVOT keyword, the simple structure of the data previously hidden, is now clearly used to provide an overview of the company’s various departments.
Also, I’ve decided to provide a sum of each departments’ salaries.

3-t-sql-pivot

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