Re: Odd pivot table type query

From: Vishal Parkar (REMOVE_THIS_vgparkar_at_yahoo.co.in)
Date: 11/23/04

  • Next message: Shawn: "Re: Odd pivot table type query"
    Date: Tue, 23 Nov 2004 05:46:46 +0530
    
    

    For documented method, you will have to make use of procedural code to
    achieve this. See following example. OR you will have to use some 3rd party
    tool to do it(www.rac4sql.com).

    Eg:
    -- sample table
    -- if object_id('tab') is not null
    -- drop table tab

    create table tab(ID int,
    SEQ_NUM int ,
    ROUTE varchar(50))
    go
    -- sample data
    insert into tab values(1 ,1 ,'AA')
    insert into tab values(1 ,2 ,'BB')
    insert into tab values(1 ,3 ,'CC')
    insert into tab values(2 ,1 ,'AA')
    insert into tab values(3 ,1 ,'VV')
    insert into tab values(3 ,2 ,'XX')
    go
    -- t-sql code for generating report
    if object_id('tempdb..#tmp') is not null
    drop table #tmp

    create table #tmp(id int, tmpval varchar(50))
    go
    declare @id int
    declare @seq_num int
    declare @route varchar(50), @f_route varchar(50)

    select @id=0, @seq_num=0, @route='', @f_route=''

    while @id is not null
    begin
       select @id=min(id) from tab where id > @id
       while @seq_num is not null
          begin
          select @seq_num=min(seq_num), @route=min(route)from tab where id = @id
          and seq_num > @seq_num
          If @seq_num is null and @id is not null
          insert into #tmp values(@id, @f_route)
          select @f_route = @f_route + case @f_route when '' then '' else ','
    end + @route
          end
       select @seq_num=0, @f_route=''
    end
    select * from #tmp
    truncate table #tmp

    -- 
    Vishal Parkar
    vgparkar@yahoo.co.in | vgparkar@hotmail.com
    

  • Next message: Shawn: "Re: Odd pivot table type query"

    Relevant Pages

    • Re: which is better... SET or SELECT?
      ... DECLARE @p int ... for each row of in the table #tmp. ... > One important difference between SET and SELECT for variable assignment is> the behaviour when no rows are returned from the SELECT statement. ... > DECLARE @id INTEGER ...
      (microsoft.public.sqlserver.programming)
    • RE: Accessing value from dynamic SQL
      ... DECLARE @table SYSNAME ... CREATE TABLE #tmp (x INT) ... > The dynamic statement will execute and return a value. ...
      (microsoft.public.sqlserver.programming)
    • Re: As a programmer of both languages...
      ... write OO or procedural code in C++. ... Your ignorance continues to astound. ... int main ... C++ is centered around the class/inheritance concept and that is OO. ...
      (comp.lang.c)
    • Re: Create a View Part 2
      ... you will have to use procedural code to do this. ... SEQ_NUM int, ... ROUTE varchar) ... select * from #tmp ...
      (microsoft.public.sqlserver.programming)
    • Re: Frequency distribution SQL statement
      ... declare @tmp table ... insert @tmp values ... > @min_value int, ... >> Dean, ...
      (microsoft.public.sqlserver.programming)