sql >> Database >  >> RDS >> Sqlserver

Hoe kan ik prognosejarengegevens van rij tot kolom weergeven?

probeer dit, je moet dynamische sql gebruiken

Declare @toyear int=2016
Declare @forcast int=10
Declare @t table (ITEM varchar(50), years int, qty int)
insert into @t
select 'TM-A' ITEM , 2013 years, 100 qty
union all
select 'TM-B' ITEM , 2013 years, 200 qty

;with CTE1 as
(
select * from @t
union all
select b.ITEM,b.years+1,b.qty+((@forcast*b.qty)/100) from @t a 
inner join cte1 b on a.ITEM=b.ITEM 
and b.years<@toyear
)
    select * from
(select  * from cte1 )t4
pivot(min(qty) for years in([2013],[2014],[2015],[2016]))pvt


  1. Bash + MySQL -d backtick probleem

  2. Koppeling implementeren in PHP-foreach-lus

  3. Operator

  4. Doorverwijzen naar startpagina na inloggen php