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

AppNo と Location が同じで、Department と Code の値が異なる複数の行 (最大 5 行) が存在する可能性があります。

以下の方法でデータを取得するストアド プロシージャが必要です。

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 ごとに、列が繰り返される 1 行のデータが必要です。

これを達成するためのいくつかの方法を提案してください。

前もってありがとう、クリシュナ

4

2 に答える 2

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 に答える
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 に答える