我正在尝试压缩存储在位掩码中的函数列表,并在枚举表中“解释”如下:
CREATE TABLE #enum([ID] [int] NOT NULL IDENTITY(1, 1) PRIMARY KEY,
[Display] [varchar] (50) NOT NULL,
[DataValue] [int] NOT NULL);
INSERT INTO [#enum] ([Display], [DataValue])
SELECT 'Function A', 1
UNION SELECT 'Function B', 2
UNION SELECT 'Function C', 4
UNION SELECT 'Function D', 8
UNION SELECT 'Function E', 16
UNION SELECT 'Function F', 32
UNION SELECT 'Function G', 64;
我的数据表看起来像这样(显然很简化):
CREATE TABLE #people ([ID] [int] NOT NULL IDENTITY(1, 1) PRIMARY KEY,
[PersonName] [varchar] (50) NOT NULL,
[Functions] [int] NOT NULL);
INSERT INTO [#people] ([PersonName], [Functions])
SELECT'Ken Hurt', 8
UNION SELECT 'Justin Case', 33
UNION SELECT 'Bill Board', 73;
现在我想在查询#people表时返回以逗号分隔的函数列表;结果如下:
ID Name Functions
1 Ken Hurt Function D
2 Justin Case Function A, Function F
3 Bill Board Function A, Function D, Function G
我一直在关注动态SQL,PIVOT
,STUFF()
和FOR XML PATH()
已经有一段时间了,但我显然太愚蠢了,不能消化它的美丽概念。能否请你帮忙?与WHILE甚至更糟糕的游标一起浏览列表似乎需要用大锤来破解坚果。谢谢!
答案 0 :(得分:4)
正确的方式:
架构“已损坏”。将其标准化。
解决方法:强>
不需要WHILE/CURSOR/PIVOT/Dynamic-SQL
。简单的按位操作将完成这项工作:
SELECT *
FROM #people p
CROSS APPLY(SELECT STUFF((SELECT ',' + e.Display
FROM #enum e
WHERE p.Functions & DataValue = DataValue
ORDER BY e.DataValue
FOR XML PATH('')), 1, 1, '')
) AS sub(result);
的 LiveDemo
强>
输出:
╔════╦═════════════╦═══════════╦══════════════════════════════════╗
║ ID ║ PersonName ║ Functions ║ result ║
╠════╬═════════════╬═══════════╬══════════════════════════════════╣
║ 1 ║ Bill Board ║ 73 ║ Function A,Function D,Function G ║
║ 2 ║ Justin Case ║ 33 ║ Function A,Function F ║
║ 3 ║ Ken Hurt ║ 8 ║ Function D ║
╚════╩═════════════╩═══════════╩══════════════════════════════════╝
答案 1 :(得分:1)
我解决了类似的问题:
SELECT p.nmbCurrentLivingSituation,
SUBSTRING(
CASE WHEN p.nmbCurrentLivingSituation & POWER(2,0) = POWER(2,0) THEN ', Two Parent Family' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,1) = POWER(2,1) THEN ', One Parent Family' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,2) = POWER(2,2) THEN ', Foster' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,3) = POWER(2,3) THEN ', Step Home' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,4) = POWER(2,4) THEN ', Institution' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,5) = POWER(2,5) THEN ', Living On Own' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,6) = POWER(2,6) THEN ', Homeless' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,7) = POWER(2,7) THEN ', Other' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,8) = POWER(2,8) THEN ', Siblings' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,9) = POWER(2,9) THEN ', Relatives' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,10) = POWER(2,10) THEN ', Spouse' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,11) = POWER(2,11) THEN ', Non-Marital Partner Childs Parent' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,12) = POWER(2,12) THEN ', Non-Marital Partner not Childs Parent' ELSE '' END
+ CASE WHEN p.nmbCurrentLivingSituation & POWER(2,13) = POWER(2,13) THEN ', Unknown' ELSE '' END
, 3, 150) As [values]
FROM tblParticipant p
结果如下:
nmbCurrentLivingSituation values
1 Two Parent Family
2 One Parent Family
NULL
0
770 One Parent Family, Siblings, Relatives
我要感谢这篇文章: In SQL, how can I split the values in a bitmask total into a comma delimited string