SQL Server 2000中多列的动态数据透视表

时间:2013-07-17 13:22:54

标签: sql sql-server sql-server-2000 pivot

我知道,在其他论坛发布相同的问题链接是不礼貌,但这里是我的问题链接:

http://www.sqlservercentral.com/Forums/Topic1474579-8-1.aspx

Microsoft SQL Server 2000 - 8.00.2039(Intel X86)2005年5月3日23:18:38版权所有(c)1988-2003 Windows NT 5.2上的Microsoft Corporation Enterprise Edition(Build 3790:Service Pack 2)

表结构和示例插入语句:

create table consumption (
code varchar(6),
prodname varchar(50),
department varchar(20),
netqty decimal(10,2),
netmrp decimal(10,2)
)

insert into consumption values ('U00180','USG JELLY IM-K','SONO',11.4,130.40)
insert into consumption values ('U00280','UNIDINE 1 LITRE SOLUTION','AKD',1.4,10.40)
insert into consumption values ('V02401','VOLINI GEL 50GM','PHYSIOTHERAPY',8,15)
insert into consumption values ('V02402','X RAY FILM DIGITAL 14"X 17"','MRI',3,26.40)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',21.46,56.78)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',10,10)

表数据:

code    prodname            department  netqty      netmrp
U00180  USG JELLY IM-K          SONO        11.40       130.40
U00280  UNIDINE 1 LITRE SOLUTION    AKD      1.40        10.40
V02401  VOLINI GEL 50GM         PHYSIOTHERAPY    8.00        15.00
V02402  X RAY FILM DIGITAL 14"X 17" MRI      3.00        26.40
U00380  TROPAC P DROPS          AKD     21.46        56.78
U00380  TROPAC P DROPS          AKD     10.00        10.00

要求输出请:

CODE    PRODNAME                               AKD                    MRI               PHYSIOTHERAPY           SONO
                                        NET QTY    NET MRP    NET QTY     NET MRP   NET QTY    NET MRP    NET QTY    NET MRP
U00180  USG JELLY IM-K                                                                                11.40      130.40 
U00280  UNIDINE 1 LITRE SOLUTION         1.40      10.40
U00380  TROPAC P DROPS                  31.46      66.78
V02401  VOLINI GEL 50GM                                                             8.00       15.00
V02402  X RAY FILM DIGITAL 14"X 17"                           3.00        26.40

很抱歉,以这种方式发布问题,但如果有人可以通过查看上述链接来帮助我,我将感谢您的帮助。

仅仅因为我认为在该论坛中,活动较少,所以我在这里尝试。

更新:问题已解决,我在上述链接中发布了解决方案。感谢所有参与并帮助过我的人。

2 个答案:

答案 0 :(得分:1)

在SQL2000中转动很痛苦,但这里是我创建的一些示例代码,用于使用游标转动多个列

DECLARE @SQL nvarchar(4000)
DECLARE @TaskName nvarchar(100)

SET NOCOUNT ON

CREATE TABLE #tblTLine (
    [DT] varchar(200)
)

CREATE TABLE #tblTasks (
    [Tasks] varchar(200)
)

INSERT INTO #tblTasks (
    [Tasks]
)
select DISTINCT
    Name
from #Pivot 


INSERT INTO #tblTLine (
    [DT]
)
select DISTINCT
    [DT]
from #Pivot 
ORDER BY DT
--WHERE Active = 1

-- Build Table
DECLARE cur CURSOR FOR

select DISTINCT
    [Tasks]
from #tblTasks

OPEN cur

FETCH NEXT FROM cur INTO @TaskName

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @SQL = 'ALTER TABLE #tblTLine ADD [' + @TaskName + '] nchar(1) NULL'
    EXEC (@SQL)

    SET @SQL = ''

    SET @SQL = 'UPDATE #tblTLine SET [' + @TaskName + '] = ''0'''
    EXEC (@SQL)

    FETCH NEXT FROM cur INTO @TaskName

END

CLOSE cur
DEALLOCATE cur

-- Update Table

DECLARE @SQLUpdate nvarchar(4000)
DECLARE @Time nvarchar(100)
DECLARE @Name nvarchar(100)
DECLARE @Active nchar(1)


DECLARE curUpdate CURSOR FOR

SELECT 
    [DT],
    [Name],
    [Active]
FROM #Pivot 
WHERE Active = 1

OPEN curUpdate

FETCH NEXT FROM curUpdate INTO @Time, @Name, @Active

WHILE @@FETCH_STATUS = 0
BEGIN

    SET @SQLUpdate = 'UPDATE #tblTLine SET [' + @Name + '] = ''1'' WHERE [DT] = ''' + @Time + ''''
    EXEC (@SQLUpdate)

    FETCH NEXT FROM curUpdate INTO  @Time, @Name, @Active

END

CLOSE curUpdate
DEALLOCATE curUpdate


SET NOCOUNT OFF

答案 1 :(得分:0)

首先没有看到SQL 2000,因为我正忙着解决问题。

create table consumption (
code varchar(6),
prodname varchar(50),
department varchar(20),
netqty decimal(10,2),
netmrp decimal(10,2)
)
;
insert into consumption values ('U00180','USG JELLY IM-K','SONO',11.4,130.40)
insert into consumption values ('U00280','UNIDINE 1 LITRE SOLUTION','AKD',1.4,10.40)
insert into consumption values ('V02401','VOLINI GEL 50GM','PHYSIOTHERAPY',8,15)
insert into consumption values ('V02402','X RAY FILM DIGITAL 14"X 17"','MRI',3,26.40)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',21.46,56.78)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',10,10)
;

DECLARE @cols1 AS NVARCHAR(MAX),
    @query1  AS NVARCHAR(MAX),
    @query2  AS NVARCHAR(MAX);

SET @cols1 = STUFF((SELECT distinct ',' + QUOTENAME(c.department) 
            FROM consumption c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')



set @query1 = 'select t.code,t.prodname,
                t.AKD as netqtyAKD,t1.AKD as netmrpAKD,
                t.MRI as netqtyMRI,t1.MRI as netmrpMRI,
                t.PHYSIOTHERAPY as netqtyPHYSIOTHERAPY,t1.PHYSIOTHERAPY as netmrpPHYSIOTHERAPY,
                t.SONO as netqtySONO,t1.SONO as netmrpSONO
                from (
            SELECT code,prodname, ' + @cols1 + ' from 
            (
                select code
                    , prodname
                    , department
                    , sum(netqty) as netqty
                from consumption
                group by code
                    , department
                    , prodname
           ) x
            pivot 
            (
                 sum(netqty)
                for department in (' + @cols1 + ')
            ) p 
            )t
            inner join
            (SELECT code,prodname, ' + @cols1 + ' from 
            (
                select code
                    , prodname
                    , department
                    , sum(netmrp) as netmrp
                from consumption
                group by code
                    , department
                    , prodname
           ) x
            pivot 
            (
                 sum(netmrp)
                for department in (' + @cols1 + ')
            ) p
            )t1
            on t.code=t1.code
            and t.prodname=t1.prodname'