tsql - Create new columns in a declared query variable in SQL Server -


i've created stored procedure in sql server 2008 r2. executes pivot table. so, need next steps 3 new columns, create in declared query variable. in these 3 columns have perform calculation.

at first, stored procedures code: update stored procedure:

begin     set nocount on;      declare @colno nvarchar(max)     declare @colsum nvarchar(max)     declare @query nvarchar(max)      set nocount on;      vals (         select distinct ds.no         qrtestview ds     )      select @colno = coalesce(@colno + ', ', '') + quotename(no)            @colsum = coalesce(@colsum + '+ ', '') + quotename(no)     vals     order no      set @query = 'select *, [bonus] + 125 [fee],                     ([bonus] + 125) * [all] [fee2]                     (                       select *,                         case when 70-(100 - quote) * 10 > -80 70-(100 - quote) * 10  else -70 end [bonus]                       (                         select *, '+@colsum+' [all]                         (                           select                               cast(isin char(12)) no,                              cast(quote char(7)) quote,                              cast(quote char(7)) q                           qrtestview                           datum >= @from_val , datum <= @to_val                         ) sel                         pivot (                           count(q)                           no in ('+ @colno +')                         ) p                        union                        select ''all'', *, '+@colsum+'[all]                       (                         select                            cast(no char(12)) no,                            count(cast(quote char(7))) quote                         qrtestview                         datum >= @from_val , datum <= @to_val                         group cast(no char(12))                       ) sel                       pivot (                         max(quote)                          no in ('+ @colno +')                       ) p                      ) sel2                   ) ff              order case when quote = ''all'' 101 else cast(quote int) end desc'      exec sp_executesql @query, n'@from_val datetime, @to_val datetime', @from_val = @from, @to_val = @to end 

to understand question, show source table:

| no      | quote | datum | de10101 | 100   | 2016-01-01 | de10121 | 100   | 2016-01-02 | de10101 | 100   | 2016-01-05 | de22034 | 98    | 2016-01-05 | de10101 | 98    | 2016-01-10 | de10121 | 80    | 2016-01-10 | de22034 | 98    | 2016-01-10 | de22034 | 80    | 2016-01-11 | de10101 | 100   | 2016-01-20 | de10121 | 80    | 2016-01-21 

and target table has 3 additional columns:

| quote | de10101 | de10121 | de22034 | | bonus | fee | fee2 | | 100   |    3    |    1    |    0    |  4  |  70   | 195 | 780  | |  98   |    1    |    0    |    2    |  3  |  50   | 175 | 525  | |  80   |    0    |    2    |    1    |  3  | -70   |  55 | 165  | |   |    4    |    3    |    3    | 10  |       |     | 1470 | 

the 3 new columns are: bonus, fee, fee2

the calculation works follows:

bonus: when 70-(100 - quote) * 10 > -80 70-(100 - quote) * 10 else -70

fee: bonus + 125

fee2: all * fee

how can define these 3 columns , calculation in query variable in stored procedure?

you need use cte

set nocount on;  declare @colno nvarchar(max) declare @sumcols nvarchar(max) declare @query nvarchar(max)   set nocount on;  vals (     select distinct ds.[no]     qrtestview ds )  select @colno = coalesce(@colno + ', ', '') + quotename([no]),        @sumcols = coalesce(@sumcols + ', ', '') + 'sum(' + quotename([no]) +')' vals order no   select @query = n' ;with cte ( select  *,          [bonus] + 125 [fee],          ([bonus] + 125) * [all] [fee2] (     select  *,             case when 70-(100 - quote) * 10 > -80 70-(100 - quote) * 10  else -70 end [bonus]     (         select  *,                  '+replace(@colno,',','+')+'  [all]         (             select  cast(no char(12)) no,                     cast(quote char(7)) quote,                     cast(quote char(7)) q             qrtestview             datum >= @from_val , datum <= @to_val             ) sel             pivot (                 count(q)                 no in ('+@colno+')             ) p         ) d     ) ff )  select * (     select  *     cte     union     select  ''all'',              '+@sumcols+'             sum([all]),             null,             null,             sum(fee2)     cte     ) t order case when quote = ''all'' 101 else cast(quote int) end desc'  exec sp_executesql @query, n'@from_val datetime, @to_val datetime', @from_val = @from, @to_val = @to 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -