sql server - Dynamic SQL: Grouping by one variable, counting another for column names -


i trying dynamic sql query, similar have appeared on forum, life of me, cannot work.

i using sql server 2008. have table series of order_ref numbers. each of these numbers has varying number of advice_refs associated it. advice_ref numbers unique (they key table). there @ least 1 advice_ref each order_ref. there bunch of columns describe information each advice_ref.

what want create table row each unique order_ref, columns each advice_ref, in ascending order. columns advice01, advice02, ....advice10, advice11, etc. not advice# columns filled in every order_ref , number of advice# columns depend on order_ref greatest number of advice_refs.

the table like:

order advice01  advice02  advice03  advice04.....   1       1         2         3           2       5         8         9        20   3       25 

the code i've tried use is:

    declare @sql nvarchar(max)     declare @pvt nvarchar(max)      select  @sql = @sql + ', coalesce(' + quotename('advice' + rownum) + ', '''') ' +  quotename('advice' + rownum),             @pvt = @pvt + ', ' + quotename('advice' + rownum)        (select case when rownum2 < 10 '0'+rownum2 when rownum2 >=10 rownum2 end [rownum]     (   select  distinct convert(varchar, row_number() over(partition order_ref order advice_ref)) [rownum2]                    [ed_dups].[dbo].[neweddupslongform]             ) rn2 ) rn      set @sql = 'select order_ref' + @sql + '                    (   select  order_ref,                                      advice_ref,                                      case when convert(varchar, row_number() over(partition order_ref order advice_ref)) < 10                                           ''advice0'' + convert(varchar, row_number() over(partition order_ref order advice_ref))                                          else ''advice'' + convert(varchar, row_number() over(partition order_ref order advice_ref))                                           end [adviceid]                                [ed_dups].[dbo].[neweddupslongform]                         ) data                         pivot                         (   max(advice_ref)                             adviceid in (' + stuff(@pvt, 1, 2, '') + ')                         ) pvt'      execute sp_executesql @sql 

sql server tells me query executed successfully, there no output. when run snippets of code, seems problem either lies in pivot statement, near + stuff(@pvt, 1, 2, '') + ') and/or in select statement, near ''advice0'' +

thanks in advance help--i've been @ days!

i think have initialize variables like

declare @sql nvarchar(max) = '' declare @pvt nvarchar(max) = '' 

or

declare @sql nvarchar(max) declare @pvt nvarchar(max)  select @sql = '', @pvt = '' 

otherwise @sql null


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -