-1

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

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 Microsoft Corporation Enterprise Edition on Windows NT 5.2(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

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

只是因为我觉得在那个论坛里,活动少,所以我在这里尝试。

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

4

2 回答 2

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
于 2013-07-17T13:55:12.257 回答
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'
于 2013-07-17T14:42:17.197 回答