【问题标题】:Rows to Columns in SQL Server 2000SQL Server 2000 中的行到列
【发布时间】:2012-10-11 16:18:14
【问题描述】:

我有这个..

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 中不提供数据透视表,我希望这里有人可以帮助我,谢谢。

【问题讨论】:

  • 欢迎使用 StackOverflow:如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行并单击“代码示例”按钮 ({ } ) 在编辑器工具栏上很好地格式化和语法突出显示它!

标签: sql cursor sql-server-2000 pivot


【解决方案1】:

由于 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

【讨论】:

  • 太棒了!非常感谢您的宝贵时间和出色的解决方案!
  • @user1738734 如果某个答案对您有帮助,请务必通过左侧的复选标记选择一个作为接受。接受的解决方案可以帮助未来的访问者,您甚至会因接受答案而获得声誉。
  • 我在实施此解决方案时遇到问题,首先,它不会删除 #colspivot 表,也无法手动删除它我不知道为什么我是管理员,如果我有很多行不起作用,因为查询 nvarchar 限制,终于有了这个结果..“操作数数据类型文本对于最大运算符无效。”
  • @user1738734 哪一列有text 数据类型?您可以将cast() 该列作为varchar(),然后选择max() 值。
  • 好的,知道了,但是现在如果我有多个 IDProspecto,它会为每个 IDProspecto 和每个评论创建一列..
【解决方案2】:

你可以试试这个例子。

准备数据:

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>=@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)

【讨论】:

  • 遇到同样的问题,受 varchar 限制,我必须查询很多 IDProspecto,如果我有多个 IDProspecto,它会为每个 IDProspecto 上的每个评论创建很多列。
  • @user1738734 结果对我来说很好。如果查询太大,可以尝试insert into tab2(...) select ...from tab1 where IDProspecto betwean 1 and 10000下次insert into tab2(...) select ...from tab1 where IDProspecto betwean 10000 and 100000等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2012-03-19
  • 2010-12-18
  • 1970-01-01
  • 2011-05-04
相关资源
最近更新 更多