A simple budget table that tracks a budget item type and a budget amount (rather than having a separate amount column for each type of item) is a good example of where this is useful...
DECLARE @BudgetItems TABLE
(CustId int, BudgetItemType varchar(20), BudgetAmount money)
--Customer 1 is living the life
INSERT @BudgetItems (CustId, BudgetItemType, BudgetAmount) VALUES (1, 'Mortgage', 1000)
INSERT @BudgetItems (CustId, BudgetItemType, BudgetAmount) VALUES (1, 'Salary', 6000)
INSERT @BudgetItems (CustId, BudgetItemType, BudgetAmount) VALUES (1, 'Food', 140)
--Customer 2 has no income
INSERT @BudgetItems (CustId, BudgetItemType, BudgetAmount) VALUES (2, 'Salary', 400)
INSERT @BudgetItems (CustId, BudgetItemType, BudgetAmount) VALUES (2, 'Food', 340)
The table ends up looking like:
SELECT * FROM @BudgetItemsAgain, very easy to ins/upd/del, but difficult to get a single client's budget into a row for something like binding a report to it as a datasource. Pivoting the table is an easy (if somewhat syntax-clumsy) solution to this:
CustId BudgetItemType BudgetAmount
------ -------------- ------------
1 Mortgage 1000.00
1 Salary 6000.00
1 Food 140.00
2 Salary 400.00
2 Food 340.00
SELECT pivotTable.*
FROM (SELECT CustId, BudgetItemType, BudgetAmount FROM @BudgetItems) AS source
PIVOT ( SUM(source.BudgetAmount) FOR BudgetItemType IN (Mortgage, Salary, Food) ) AS pivotTable
CustId Salary Mortgage Food
------ ------- -------- ------
1 6000.00 1000.00 140.00
2 400.00 NULL 340.00
0 comments:
Post a Comment