Roll up of a column in dynamic query

  • HI

    This is data in the table

    ClientIdAdTagIdNameTotal

    1D147American1000

    1D247American500

    1D347American300

    1D448Hispanic1000

    1D548Hispanic200

    This is the result when I pivot the table

    ClientId TagId NameD1D2D3D4D5

    147American10005003000 0

    148Hispanic0 0 0 1000200

    you can copy the following code

    create table #TestTable

    (

    ClientIdint

    ,Advarchar(10)

    ,TagIDint

    ,[Name]varchar(10)

    ,Totalint

    )

    Insert #TestTable values (1,'D1',47,'American',1000)

    Insert #TestTable values (1,'D2',47,'American',500)

    Insert #TestTable values (1,'D3',47,'American',300)

    Insert #TestTable values (1,'D4',48,'Hispanic',1000)

    Insert #TestTable values (1,'D5',48,'Hispanic',200)

    DECLARE @columns VARCHAR(8000)

    DECLARE @columns2 VARCHAR(8000)

    DECLARE @sql NVARCHAR(MAX)

    SET @Columns = SUBSTRING((SELECT DISTINCT ',['+Ad+']' FROM #TestTable GROUP BY Ad FOR XML PATH('')),2,8000)

    SET @Columns2 = SUBSTRING((SELECT DISTINCT ',ISNULL(['+Ad+'],0) AS ['+Ad+']' FROM #TestTable GROUP BY Ad FOR XML PATH('')),2,8000)

    SET @SQL = 'SELECT Clientid, TagId, [Name],' + @Columns2 + ' FROM

    (Select Clientid, TagId, [Name], ad, Total from #TestTable ) SourceData

    PIVOT

    (min(Total) for ad in ('+@Columns+')) pivottable

    Order by ClientId, TagId '

    EXEC(@sql)

    But I want the the roll up of the colums.. The desired output would be like this.

    Cid TagId Name D1 D2 D3D4 D5

    147American100050030000

    147American100050030000

    147 ALL 2000 1000 600 0 0

    148Hispanic0001000200

    148Hispanic0001000200

    148 ALL 0 0 0 2000 400

    please help me

  • google for Cube and Rollup in sql, might be that's your answer:w00t:

  • Yeah Roll up works here. I have already tried.. but it is not working in inner query. So I have taken another outer select query and got the desired result.. thank u

  • Now I have to get the row sum of each column from D1 to D4.

    Example: the desired result set would be like the below

    Cid TagId Name D1 D2 D3 D4 D5 tot(D1andD2) tot(D3andD4)

    1 47 American 1000 500 300 0 0 1500 300

    1 47 American 1000 0 300 0 500 1000 800

    Any idea?

  • create table #TestTable

    (

    ClientId int

    ,Ad varchar(10)

    ,TagID int

    ,[Name] varchar(10)

    ,Total int

    )

    Insert #TestTable values (1 , 'D1', 47, 'American', 1000)

    Insert #TestTable values (1 , 'D2', 47, 'American', 500)

    Insert #TestTable values (1 , 'D3', 47, 'American', 300)

    Insert #TestTable values (1 , 'D4', 48, 'Hispanic', 1000)

    Insert #TestTable values (1 , 'D5', 48, 'Hispanic', 200)

    DECLARE @columns VARCHAR(8000)

    DECLARE @columns2 VARCHAR(8000)

    declare @TotalXint

    DECLARE @sql NVARCHAR(MAX)

    SET @Columns = SUBSTRING((SELECT DISTINCT ',['+Ad+']' FROM #TestTable GROUP BY Ad FOR XML PATH('')),2,8000)

    SET @Columns2 = SUBSTRING((SELECT DISTINCT ',ISNULL(['+Ad+'],0) AS ['+Ad+']' FROM #TestTable GROUP BY Ad FOR XML PATH('')),2,8000)

    set @TotalX = 3

    SET @SQL ='SELECT Clientid, TagId, [Name],' + @Columns2 + '

    FROM

    (Select Clientid, TagId, [Name], ad, Total from #TestTable ) SourceData

    PIVOT

    (min(Total) for ad in ('+@Columns+')) pivottable

    Order by ClientId, TagId '

    --EXEC(@sql)

    --drop table #TestTable

    CREATE TABLE #Temp1

    (

    clientid int,

    tagid int,

    tname varchar(max),

    d1 int,

    d2 int,

    d3 int,

    d4 int,

    d5 int

    )

    insert into #temp1

    EXEC(@sql)

    declare @counter varchar(25)

    SET @Counter = 1

    DECLARE @ColumnCreator NVARCHAR(MAX)

    WHILE@Counter <= @TotalX

    BEGIN

    SET @ColumnCreator = 'ALTER TABLE#Temp1 ADD DTotal'+ @Counter + ' DECIMAL(18,2)'

    EXEC(@ColumnCreator)

    SET@Counter = @Counter + 1

    END

    DECLARE @TotalCreator NVARCHAR(MAX)

    declare @i varchar(5)

    set @i = 1

    declare @i1 varchar(5)

    set @i1 = 2

    SET @TotalCreator ='update #temp1 set dtotal'+@i+' = d'+ @i + '+ d'+ @i1

    exec(@TotalCreator)

    set @i = @i+1

    set @i1 = @i1+1

    SET @TotalCreator ='update #temp1 set dtotal'+@i+' = d'+ @i + '+ d'+ @i1

    exec(@TotalCreator)

    set @i = @i+1

    set @i1 = @i1+1

    SET @TotalCreator ='update #temp1 set dtotal'+@i+' = d'+ @i + '+ d'+ @i1

    exec(@TotalCreator)

    set @i = @i+1

    set @i1 = @i1+1

    select * from #temp1

    drop table #TestTable

    drop table #Temp1

  • Thank you very much for your help... but I cant create a table with the columns D1, D2 etc like..

    CREATE TABLE #Temp1

    (

    clientid int,

    tagid int,

    tname varchar(max),

    d1 int,

    d2 int,

    d3 int,

    d4 int,

    d5 int

    )

    since the number of those columns will change. every month the additional column will add to the table like D6, D7 etc.

  • you can make dynamic.

  • have got the desired result.. Thank you very much

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply