我正在尝试创建一个动态的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 |
答案 0 :(得分:1)
这里有很多免责声明。依靠你只关心所有4个值都很重要的案例。我没有测试如果其中一个案例的第五个客户端不匹配,或者如果有两个以上的客户使用同一组客户端会发生什么。它适用于您提供的样本数据,除了您希望在每个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
答案 1 :(得分: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