【发布时间】:2021-03-05 08:18:58
【问题描述】:
这是我的桌子。每列都表示为子查询的结果。
id | A | B | C | (and many columns)
---+-------+-------+-------+----------+--------+-----+-...
1 | 1a3b | 1a2b | 4a1b | |
2 | 7a3b | 3a7b | 7a7b | |
3 | 2a3b | 1a3b | 3a6b | |
4 | 6a3b | 6a3b | 6a3b | |
我的表中有很多列,不仅是 A、B 和 C。
结果由CONCAT方法生成。
SELECT CONCAT(A,B,C,...) FROM myTable
这就是我得到的。不是我想要的答案。
id | A | B | C | result |
---+-------+-------+-------+---------------+
1 | 1a3b | NULL | 4a1b | 1a3b4a1b |
2 | 7a3b | 3a7b | 7a3b | 7a3b3a7b7a3b |
3 | 2a3b | 1a3b | 3a6b | 2a3b1a3b3a6b |
4 | 6a3b | 6a3b | 6a3b | 6a3b6a3b6a3b |
我找不到"group by" 行并获得如下结果以删除(或不连接)重复值的方法。有没有办法做到这一点?
id | A | B | C | result |
---+-------+-------+-------+---------------+
1 | 1a3b | NULL | 4a1b | 1a3b4a1b |
2 | 7a3b | 3a7b | 7a3b | 7a3b3a7b |
3 | 2a3b | 1a3b | 3a6b | 2a3b1a3b3a6b |
4 | 6a3b | 6a3b | 6a3b | 6a3b |
【问题讨论】:
-
您的 SQL Server 版本是多少?
-
Each column is representing as a result of a subquery.和There are many rows in my table, not only A, B and C.是什么意思? -
@Squirrel 子查询是字符串的结果,因此您可以像处理字符串一样处理它。对于第二句话,我犯了一个错误。 “我的桌子上有很多
column,不只是A、B和C。”而不是rows。 -
@Zhorov 我的版本是“Microsoft SQL Server 2016 (SP1) (KB3182545) - 13.0.4001.0 (X64) Oct 28 2016 18:17:30 版权所有 (c) Microsoft Corporation Standard Edition (64-位) 在 Windows Server 2012 R2 Datacenter 6.3
(Build 9600: ) (Hypervisor) "
标签: sql sql-server group-by concatenation row