行中的数据列

时间:2013-04-02 06:02:55

标签: mysql stored-procedures

我有一个场景,在哪里,我有一张记录表:

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,具有不同的部门和代码值。

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

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,我们需要一行中的数据,重复列。

请告诉我一些方法来实现这一目标。

先谢谢,克里希纳

2 个答案:

答案 0 :(得分:0)

MySQL中没有PIVOT命令,但有一些解决方法 - Pivot table basics: rows to columns...MySQL pivot tables (transform rows to columns)

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

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

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

答案 1 :(得分: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