是否有人可以帮助我实现这个查询:我需要携带所有的ID为的值为1,每个字母:SQL查询来选择特定的列值,并且将它们连接起来
回答
这是一个两步过程。首先,你需要UNPIVOT列以行:
SELECT upvt.ID, Letters
FROM T
UNPIVOT
( Value
FOR Letters IN ([A], [B], [C], [D], [E], [F])
) upvt
WHERE upvt.Value = 1;
这给:
ID Letters
10 A
10 C
10 E
10 F
...
然后你需要连接的ID的从这个结果:”
WITH Unpivoted AS
( SELECT upvt.ID, Letters
FROM T
UNPIVOT
( Value
FOR Letters IN ([A], [B], [C], [D], [E], [F])
) upvt
WHERE upvt.Value = 1
)
SELECT u.Letters,
IDs = STUFF(( SELECT ', ' + CAST(u2.ID AS VARCHAR(10))
FROM Unpivoted u2
WHERE u.Letters = u2.Letters
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM Unpivoted u
GROUP BY u.Letters;
其中给出:
Letters IDs
A 10, 20, 50
B 20, 40
C 10, 20, 30, 40, 50
D 30, 40
E 10, 50
F 10, 20, 40
非常感谢。我在哪里可以指定Letter列的值。例如,A的列名是Letter_A。我怎么能把它作为“A”而不是结果中的列名“Letter_A”? – user3314399
只需使用替换,它说'SELECT u.Letters'使用'SELECT Letters = REPLACE(u.Letters,'Letter_','')' – GarethD
真棒谢谢你,最后一个问题。我如何将它插入临时表?它不让我这样做,因为查询使用“with”。 – user3314399
select distinct 'A',
(select cast(id as varchar)+',' from letters where a=1 for xml path('')) ids
from letters where a=1
union all
select distinct 'B',
(select cast(id as varchar)+',' from letters where b=1 for xml path('')) ids
from letters where b=1
union all
select distinct 'C',
(select cast(id as varchar)+',' from letters where c=1 for xml path('')) ids
from letters where c=1
union all
select distinct 'D',
(select cast(id as varchar)+',' from letters where d=1 for xml path('')) ids
from letters where D=1
union all
select distinct 'E',
(select cast(id as varchar)+',' from letters where e=1 for xml path('')) ids
from letters where e=1
union all
select distinct 'F',
(select cast(id as varchar)+',' from letters where f=1 for xml path('')) ids
from letters where f=1
这里有两个问题:第一,该表是不归,让你真正需要首先做一个额外的步骤来创建标准化数据的临时表:
第一步:
select id, 'A' as letter
from mytable where a=1
union
select id, 'B'
from mytable where b=1
union
select id, 'C'
from mytable where c=1
union
select id, 'D'
from mytable where d=1
union
select id, 'E'
from mytable where e=1
union
select id, 'F'
from mytable where f=1
然后,你需要将多个ID塞进一个字段。你可以用(欺骗性地命名)“For XML”来做到这一点。
喜欢的东西:
select letter, id + ', ' as [text()]
from
(
select id, 'A' as letter
from mytable where a=1
union
select id, 'B'
from mytable where b=1
union
select id, 'C'
from mytable where c=1
union
select id, 'D'
from mytable where d=1
union
select id, 'E'
from mytable where e=1
union
select id, 'F'
from mytable where f=1
) q
group by letter
for XML path(''))
我认为这是可行的。
- 1. SQL服务器中:选择4个非空列,将它们连接起来
- 2. Sql查询来选择值并按系列对它们进行分组
- 3. 读取.part文件并将它们全部连接起来
- 4. SQL查询来连接表
- 5. SQL查询来连接表
- 6. SQL查询来选择总
- 7. 将原始值与MySQL中的查询值连接起来
- 8. SQL连接查询选择
- 9. SQL查询来选择即将到期
- 10. SQL查询来获取它们使用特定的列作为外键
- 11. SQL LINQ查询:选择特定列
- 12. 使用选择查询来源连接表的值
- 13. SELECT * SQL查询VS选择特定的列SQL查询
- 14. 创建excel宏来检查列值并将它们相加
- 15. 如何使用扫描仪从键盘读取一系列字符串,并将它们连接在一起并将它们连接起来
- 16. SQL选择独特的元素,并将它们与上两期
- 17. Farseer 3.3 DebugViewXNA - 将它连接起来
- 18. Cypher查询以查找现有节点并将它们关联起来。
- 19. 动态sql从列中使用连接选择特定值
- 20. 设计模式来决定哪些SQL查询来选择
- 21. 接收来自其它类的特定值,其不连接
- 22. 从[]数组中取元素并将它们连接起来的算法
- 23. 匹配不同的列并将它们用python结合起来
- 24. 查询与子选择连接挂起
- 25. REGEX Str替换 - 查找链接文本并将它们链接起来
- 26. 将Oracle与SQL Server连接起来
- 27. 访问SQL查询来连接行
- 28. SQL查询来连接两个表
- 29. SQL查询来选择属性
- 30. 获取SQL查询来选择
看起来你需要使用SQL PIVOT \ UNPIVOT,看看这是否有帮助http://stackoverflow.com/questions/3241450/sql-pivot-with-multiple-columns – user3193257