2

我有一个场景,在其中,我有一个包含记录的表:

AppNo   Location    Department  Code
1       NY          XYZ         1
1       NY          ABC         2
1       NY          PQR         3
2       TX          XYZ         1
2       TX          ABC         2

可以有多个行(最多 5 行)具有相同的 AppNo 和 Location 具有不同的 Department 和 Code 值。

我们需要一个以下列方式获取数据的存储过程:

AppNo   Location    Department  Code    Location    Department  Code    Location    Department  Code    Location    Department  Code    Location    Department  Code
1       NY          XYZ         1       NY          ABC         2       NY          PQR         3       
2       TX          XYZ         1       TX          ABC         2

基本上,对于每个 AppNo,我们需要一行中的数据,列重复。

请建议我一些方法来实现这一点。

在此先感谢,克里希纳

4

2 回答 2

0

MySQL 中没有 PIVOT 命令,但有一些解决方法 -数据透视表基础:行到列...MySQL 数据透视表(将行转换为列)

对于你的情况,你可以使用这样的东西 -

SELECT AppNo,
  MAX(IF(code = 1, Location, NULL)) Location,
  MAX(IF(code = 1, Department, NULL)) Department,
  1 Code1,
  MAX(IF(code = 2, Location, NULL)) Location,
  MAX(IF(code = 2, Department, NULL)) Department,
  2 Code2,
  MAX(IF(code = 3, Location, NULL)) Location,
  MAX(IF(code = 3, Department, NULL)) Department,
  3 Code3,
  MAX(IF(code = 4, Location, NULL)) Location,
  MAX(IF(code = 4, Department, NULL)) Department,
  4 Code4,
  MAX(IF(code = 5, Location, NULL)) Location,
  MAX(IF(code = 5, Department, NULL)) Department,
  5 Code5
FROM <table name>
GROUP BY AppNo

PS 您可以从存储的例程中运行此查询。

于 2013-04-02T06:18:17.610 回答
0

程序将继续进行sql server 2008 r2(没有任何知识,MYSQL很弱。)

--EXEC ABC
CREATE PROCEDURE ABC
AS
BEGIN
    BEGIN TRY

        if OBJECT_ID('tempdb..#app') is not null
        begin
            drop table #app
        end

        create table #app
        (
            AppNo int,
            Location varchar(10),
            Department varchar(10),
            Code int
        )

        insert into #app
        values
        (1       ,'NY'          ,'XYZ1'     ,    1),
        (1       ,'NY'          ,'ABC1'     ,    2),
        (1       ,'NY'          ,'PQR1'     ,    3),
        (2       ,'TX'          ,'XYZ'     ,    1),
        (2       ,'TX'          ,'ABC'     ,    2)

        --select * from #app

        if OBJECT_ID('tempdb..#t') is not null
        begin
            drop table #t
        end

        create table #t
        (
            AppNo int,
            Location1 varchar(10),
            Department1 varchar(10),
            Code1 int,
            Location2 varchar(10),
            Department2 varchar(10),
            Code2 int,
            Location3 varchar(10),
            Department3 varchar(10),
            Code3 int,
            Location4 varchar(10),
            Department4 varchar(10),
            Code4 int,
            Location5 varchar(10),
            Department5 varchar(10),
            Code5 int,
        )

        insert into #t
        (   
            AppNo,
            Location1,
            Department1,
            Code1,
            Location2,
            Department2,
            Code2,
            Location3,
            Department3,
            Code3,
            Location4,
            Department4,
            Code4,
            Location5,
            Department5,
            Code5
        )

        select 
            DISTINCT
            A.AppNo AS AppNo,
            A1.Location AS Location1,
            A1.Department AS Department1,
            A1.Code AS Code1,
            A2.Location AS Location1,
            A2.Department AS Department1,
            A2.Code AS Code1,
            A3.Location AS Location1,
            A3.Department AS Department1,
            A3.Code AS Code1,
            A4.Location AS Location1,
            A4.Department AS Department1,
            A4.Code AS Code1,
            A5.Location AS Location1,
            A5.Department AS Department1,
            A5.Code AS Code1
        from #app A
        INNER JOIN #app A1 ON A.AppNo=A1.AppNo AND A1.Code=1
        LEFT JOIN #app A2 ON A.AppNo=A2.AppNo AND A2.Code=2
        LEFT JOIN #app A3 ON A.AppNo=A3.AppNo AND A3.Code=3
        LEFT JOIN #app A4 ON A.AppNo=A4.AppNo AND A4.Code=4
        LEFT JOIN #app A5 ON A.AppNo=A5.AppNo AND A5.Code=5

        SELECT * FROM #t
    END TRY
    BEGIN CATCH
        --YOUR ERROR
    END CATCH
END
于 2013-04-02T08:58:19.027 回答