Unpivot - Pivot

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

declare @T table
(
  Sale varchar(10),
  [Month] int,
  [Count] int,
  Budgeted money,
  Actual money
)

insert into @T values
('NewSale',     1,      120,     45.23,     50.10),
('NewSale',     2,      30,      3.10,      1.2),
('NewSale',     3,      70,      45.00,     100.32)

select [Type], [1], [2], [3]
from
(
  select [Month],
         cast([Count] as money) as [Count],
         Budgeted,
         Actual
  from @T
) as T
unpivot
(
  Value for [Type] in ([Count], Budgeted, Actual)
) as U
pivot
(
  sum(Value) for [Month] in ([1], [2], [3])
) as P

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results