2012-10-11 91 views
3

我有這個..行分列在SQL Server 2000

IDProspecto | IDObservacionCustomer | Observacion 
---------------------------------------------------------  
    2204078 | 275214    | 03/9 Hable con Claudia me informa que Roberto ya se termino le deje.. 
    2204078 | 294567    | 19/09 SOLICITAN LLAME MAÑANA A ALEJANDRO 
    2204078 | 295310    | 20/09 se envia mail a adrian 
    2204078 | 304102    | CIA SOLICITA NO INSTALE EQUIPO 

而且我希望有這樣...

idprospecto | observacion1   | observacion2   | observacion3  | observacion4 | observacionN 
-----------------------------------------------------------------------------------------  
    2204078 | 03/09 Hable con clau... | 19/09 solicitan llame... | 20/09 se envia... | CIA solicita.. | ... 

我讀了很多關於這一點,但我發現很難實現,因爲我從來沒有使用過光標,並且數據透視在SQL Server 2000中不可用,所以我希望這裏有人能幫助我,謝謝。

+0

歡迎StackOverflow上:如果您發佈的代碼,XML或數據樣本,** **請在高亮文本編輯器的線,然後點擊「代碼(編輯器)工具欄上的「樣本」按鈕(「{}」),以精確地格式化和語法突出顯示它! –

回答

3

由於SQL Server 2000中不具備PIVOT功能,你應該能夠使用類似下面的內容:

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) 

SQL Fiddle With Demo

+0

令人驚歎!,非常感謝你的時間和你的偉大的解決方案! – user1738734

+0

@ user1738734如果答案對您有幫助,請確保通過左側的複選標記選擇一個。接受的解決方案有助於未來的訪問者,甚至可以獲得接受答案的聲譽。 – Taryn

+0

我有問題實施此解決方案,首先,它不會丟棄#colspivot表,並不能放棄它手動我不知道爲什麼我管理員,如果我有很多行不工作,因爲查詢nvarchar限制,並最終有這個結果..「操作數數據類型文本對於最大操作符無效。「 – user1738734

1

你可以試試這個例子。

準備數據:

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') 

我們需要identity場,所以我創建新表,從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 

運行查詢:

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>[email protected] 
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 @[email protected]+1 
end 

select @SqlSel, @SqlJoin 

exec(@SqlSel+' '+ @SqlJoin) 
+0

另一個偉大的解決方案!,非常感謝你的時間! – user1738734

+0

同樣的問題,由varchar限制,我不得不查詢大量的IDProspecto,如果我有多個IDProspecto,它會爲每個IDProspecto上的每條評論創建大量的列。 – user1738734

+0

http://sqlfiddle.com/#!3/b8df5/1 – user1738734