0

我正在尝试创建一个动态 Pivot,但似乎无法使其正常工作。

桌子:

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 个值都很重要的情况。我没有测试如果其中一个案例的第 5 个客户不匹配,或者如果有两个以上的案例具有相同的一组客户,会发生什么。它适用于您提供的示例数据,除了您希望在每个 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 回答