0

動的ピボットを作成しようとしていますが、機能しないようです。

テーブル:

SELECT [CaseID], [ClientID]
from [RegData]

現在の返品:

CaseID--|ClientID| RecID
------------------------
7845107 |115172  |410
7845107 |164570  |1197
7845107 |115655  |416
7845107 |154244  |425
7856846 |116684  |151
7856846 |112354  |445
7455444 |115172  |492
7455444 |164570  |518
7455444 |115655  |1297
7455444 |154244  |681

私はそれが次のように戻る必要があります:

CaseID--|Val1----|Val2----|Val3----|Val4----|
7845107 |115172  |164570  |115655  |154244  |
7856846 |116684  |112354  |        |        |
7455444 |115172  |164570  |115655  |154244  |

次に、CaseIDが異なるが、VAL1、VAL2、VAL3などが同じであるインスタンスを見つける必要があります。

CaseID--|Val1----|Val2----|Val3----|Val4----|
7845107 |115172  |164570  |115655  |154244  |
7455444 |115172  |164570  |115655  |154244  |
4

2 に答える 2

1

ここには多くの免責事項があります。4 つの値すべてが重要な場合のみを気にする必要があります。ケースの 1 つに一致しない 5 番目のクライアントがあった場合、または同じクライアントのセットを持つケースが 2 つ以上ある場合にどうなるかはテストしていません。Value1 / Value2 列などのそれぞれに表示されると予想される値が、サンプル出力とは異なる順序で表示されることを除いて、提供したサンプル データに対して機能します。

DECLARE @x TABLE(CaseID INT, ClientID INT);

INSERT @x SELECT 7845107,115172 UNION ALL SELECT 7845107,164570
UNION ALL SELECT 7845107,115655 UNION ALL SELECT 7845107,154244
UNION ALL SELECT 7856846,116684 UNION ALL SELECT 7856846,112354
UNION ALL SELECT 7455444,115172 UNION ALL SELECT 7455444,164570
UNION ALL SELECT 7455444,115655 UNION ALL SELECT 7455444,154244;

;WITH x AS
(
  SELECT CaseID, ClientID, rn = ROW_NUMBER() OVER 
  (PARTITION BY CaseID ORDER BY ClientID) 
  FROM @x
),
y AS 
(
    SELECT x.CaseID, x.ClientID, x.rn
    FROM x INNER JOIN x AS x2
    ON x.CaseID <> x2.CaseID
    AND x.ClientID = x2.ClientID
    AND x.rn = x2.rn
)
SELECT CaseID, 
    Value1 = MAX(CASE WHEN rn = 1 THEN ClientID END),
    Value2 = MAX(CASE WHEN rn = 2 THEN ClientID END),
    Value3 = MAX(CASE WHEN rn = 3 THEN ClientID END),
    Value4 = MAX(CASE WHEN rn = 4 THEN ClientID END)
FROM y
GROUP BY CaseID;

結果:

CaseID   Value1  Value2  Value3  Value4
-------  ------  ------  ------  ------
7455444  115172  115655  154244  164570
7845107  115172  115655  154244  164570
于 2012-05-16T02:53:17.157 に答える
0

これが私が思いついたものです。あまり効率的ではありませんが、うまくいきます...

テーブルを作成しました。私はたまたま、それらがセット内で同じケースが9つ以下であることを知っています。したがって、Val9.

DECLARE 
    @totalRecords int,
    @counter int,
    @QueryCaseID int,
    @QueryClientID int,
    @queryVal1 int,
    @queryVal2 int,
    @queryVal3 int,
    @queryVal4 int,
    @queryVal5 int,
    @queryVal6 int,
    @queryVal7 int,
    @queryVal8 int,
    @queryVal9 int

Set @totalRecords = (Select count(*) from RegData) +1 
Set @counter = 1

-- Get the rows in the table and put them into the 'RegData_reformat' table
While @counter < @totalRecords
    Begin

        -- get the CASEID to cross query to reformatted table
        SET @QueryCaseID = (Select CaseID from RegData where RecID = @counter)

        -- get the ClientId to cross query to reformatted table
        SET @QueryClientID = (Select ClientID from RegData where RecID = @counter)



        -- assign the columns to variables
        SET @queryVal1 = (Select Val1 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal2 = (Select Val2 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal3 = (Select Val3 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal4 = (Select Val4 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal5 = (Select Val5 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal6 = (Select Val6 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal7 = (Select Val7 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal8 = (Select Val8 from RegData_reformat where CaseID = @QueryCaseID)
        SET @queryVal9 = (Select Val9 from RegData_reformat where CaseID = @QueryCaseID)


        --determine which column to insert the data into


--  Insert the data into the formatted table

        IF @queryVal1 IS NULL
            BEGIN
                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val1] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 2 test
        else IF @queryVal2 IS NULL
            BEGIN
                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val2] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 3 test
        else IF @queryVal3 IS NULL
            BEGIN
                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val3] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 4 test
        else IF @queryVal4 IS NULL
            BEGIN
                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val4] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 5 test            
        else IF @queryVal5 IS NULL
            BEGIN

                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val5] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 6 test
        else IF @queryVal6 IS NULL
            BEGIN

                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val6] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 7 test
        else IF @queryVal7 IS NULL
            BEGIN

                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val7] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 8 test
        else IF @queryVal8 IS NULL
            BEGIN

                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val8] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END

    -- Column 9 test
        else IF @queryVal9 IS NULL
            BEGIN

                UPDATE [4DISCSReporting].[dbo].[RegData_reformat]
                SET [Val9] = @QueryClientID
                WHERE CaseID = @QueryCaseID
            END



        ELSE
            BEGIN
                print 'Ran out of columns to enter data into'
                Select 'FATAL ERROR: ran out of columns to enter data into' as Error
            END



    set @counter = @counter +1  


    END
于 2012-05-17T04:14:21.753 に答える