我有2个表,我想编写一个查询来计算列结果,该列将第一张表中的每一列与第二张表进行比较。
DECLARE @result TABLE (A INT, B INT, C INT, D INT, E INT, F INT, G INT)
INSERT INTO @result ( A, B, C, D, E, F, G)
VALUES ( 2, 14,19,20,35,41,15)
DECLARE @bet TABLE (A INT, B INT, C INT, D INT, E INT, F INT)
INSERT INTO @bet ( A, B, C, D, E, F)
VALUES( 2, 14,19,20,35,41 ),
( 5, 14,15,20,33,46 ),
答案 0 :(得分:0)
这应该可以完成工作:
SELECT STUFF((
SELECT ','+CAST(X as varchar(4)) [*]
FROM (VALUES (A),(B),(C),(D),(E),(F)) T(X)
WHERE X IN (
SELECT X
FROM @result
UNPIVOT (X FOR Y IN (A,B,C,D,E,F,G)) P
)
ORDER BY X FOR XML PATH('')), 1, 1, '')
FROM @bet
结果:
Result
-------------------
2,14,19,20,35,41
14,15,20
NULL