我在MSSQL Server中有两个表,如下所示
表1:在Col2中具有逗号分隔值的表
--------------------------------------------------
| S. No | Col1 | Col2 | Col3 |
--------------------------------------------------
| 1 | AAAA |A2,A1,C2,B2,C1| X1 |
--------------------------------------------------
| 2 | BBBB | B2 | X2 |
--------------------------------------------------
| 3 | CCCC| B2,C2,C1,D1, B1| X3 |
--------------------------------------------------
| 4 | DDDD | C1 | X4 |
--------------------------------------------------
| 5 | EEEE |C2,C1,D1 | X5 |
--------------------------------------------------
表2:优先事项列于表2
----------------------------------------
| Priority | Col1 |
----------------------------------------
| 1 | D1 |
----------------------------------------
| 2 | D2 |
----------------------------------------
| 3 | B1 |
----------------------------------------
| 4 | C2 |
----------------------------------------
| 5 | C2 |
----------------------------------------
| 6 | B2 |
----------------------------------------
| 7 | A1 |
----------------------------------------
| 8 | A2 |
----------------------------------------
预期输出:创建第三个表,根据表2中的优先级,输出表应该只具有Col2中的最高优先级值,如下所示
--------------------------------------------------
| S. No | Col1 | Col2 | Col3 |
--------------------------------------------------
| 1 | AAAA | C2 | X1 |
--------------------------------------------------
| 2 | BBBB | B2 | X2 |
--------------------------------------------------
| 3 | CCCC | D1 | X3 |
--------------------------------------------------
| 4 | DDDD | C1 | X4 |
--------------------------------------------------
| 5 | EEEE | D1 | X5 |
--------------------------------------------------
答案 0 :(得分:2)
由于数据已加载到Table_1和Table_2中,因此可以在SELECT语句中使用以下函数来标识具有最高优先级的值。该函数接受一个字符串,将其拆分并根据表2中的优先级返回最高的字符串。 如果你想这样做是SSIS,将SELECT嵌入Source组件中。
CREATE FUNCTION fn_GetPriority (@S varchar(max))
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @Split char(1),
@X xml,
@Output varchar(10)
DECLARE @Tab AS TABLE(Col1 varchar(50))
SELECT @Split = ','
SELECT @X = CONVERT(xml,' <root> <s>' + REPLACE(@S,@Split,'</s> <s>') + '</s> </root> ')
INSERT INTO @Tab
SELECT [Value] = T.c.value('.','varchar(20)')
FROM @X.nodes('/root/s') T(c) --AS T1
SELECT @Output =
(SELECT T3.Col1 AS Col1
FROM Table_2 T3
JOIN (
SELECT MAX([Priority]) AS [Priority]
FROM Table_2 T1
JOIN @Tab T2
ON T1.Col1 = T2.Col1
) AS T4
ON T3.Priority = T4.Priority
)
RETURN @Output
END
要插入表3,请使用以下insert语句:
INSERT INTO Table_3 ([S. No], Col1, Col2, Col3)
SELECT [S. No], Col1, [dbo].[fn_GetPriority](Col2), Col3 FROM Table_1