Rows to Columns in SQL Server 2000

前端 未结 2 1555
执笔经年
执笔经年 2020-12-12 00:16

I have this..

IDProspecto | IDObservacionCustomer | Observacion
---------------------------------------------------------    
  2204078   | 275214                    


        
相关标签:
2条回答
  • 2020-12-12 00:55

    You can try this example.

    Prepare data:

    create table tab1
    (
      IDProspecto  int,
      IDObservacionCustomer int, 
      Observacion varchar(130)
    )    
    
    insert into tab1
    values (2204078,275214 ,'03/9 Hable con Claudia me informa que Roberto ya se termino le deje..' )
    
    insert into tab1
    values (2204078,294567   ,'19/09 SOLICITAN LLAME MAÑANA A ALEJANDRO   ' )
    
    insert into tab1
    values (2204078,295310 ,'20/09 se envia mail a adrian' )
    
    insert into tab1
    values (2204078,304102 ,'CIA SOLICITA NO INSTALE EQUIPO' )
    

    We need identity field, so I create new table, and copy data from tab1.

    create table tab2
    (
      id int identity,  
      IDProspecto  int,
      IDObservacionCustomer int, 
      Observacion varchar(130)
    )
    
    insert into tab2(IDProspecto,IDObservacionCustomer,Observacion)
    select IDProspecto,IDObservacionCustomer,Observacion from tab1
    

    Run query:

    declare @max int, @inc int, @SqlSel varchar(2000),@SqlJoin varchar(2000), @Sql varchar(2000)
    
    select @max = max(cnt) from (
    select count(1) as cnt
      from tab1
    ) T
    
    select @inc = 1
    select @SqlSel = 'select distinct t.IDProspecto '
    select @SqlJoin = 'from tab2 t'
    while @max>=@inc
    begin
      select @SqlSel= @SqlSel+', tab2'+convert(varchar,@inc)+'.Observacion as o'+convert(varchar,@inc)
      select @SqlJoin = @SqlJoin+' left join tab2 as tab2'+convert(varchar,@inc)+' on t.IDProspecto = tab2'+convert(varchar,@inc)+'.IDProspecto and  tab2'+convert(varchar,@inc)+'.id='+convert(varchar,@inc)
      select @inc=@inc+1
    end
    
    select @SqlSel, @SqlJoin
    
    exec( @SqlSel+' '+ @SqlJoin)
    
    0 讨论(0)
  • 2020-12-12 01:01

    Since SQL Server 2000 does not have the PIVOT function, you should be able to use something similar to the following:

    DECLARE @query AS NVARCHAR(4000)
    DECLARE   @rowCount as int
    DECLARE   @pivotCount as int
    DECLARE   @pivotRow as varchar(10)
    
    set @rowCount = 1
    set @pivotRow = ''
    
    create table #colsPivot
    (
      id int IDENTITY(1,1),
      name varchar(20),
      CustId int
    )
    
    insert into #colsPivot
    select 'Observacion', IDObservacionCustomer
    from yourtable
    
    set @pivotCount= (select COUNT(*) from #colsPivot) 
    
    -- reset rowcount
    set @rowCount = 1
    set @query = ''
    
    ---- create the CASE string
    while @rowCount <= @pivotCount
        begin
            set @pivotRow = (select Top 1 CustId from #colsPivot)
    
            set @query = @query + ', max(case when IDObservacionCustomer = ''' + @pivotRow + ''' then Observacion end) as ''Observacion_' + cast(@rowCount as varchar(10)) + ''''
    
            delete from #colsPivot where CustId = @pivotRow
    
            if @rowCount <= @pivotCount
                begin
                    set @rowCount = @rowCount + 1
                end
        end
    
    -- add the rest of the SQL Statement
    set @query = 'SELECT IDProspecto ' + @query + ' from yourtable group by IDProspecto'
    
    exec(@query)
    

    See SQL Fiddle With Demo

    0 讨论(0)
提交回复
热议问题