将列中的类似数据合并到单行中
我正在尝试合并作为读取 .CSV 的 SQL Server 2005 查询的结果集的行的部分内容。这是我所拥有的数据的简化版本:
objectID | value1 | value2
_________________________________
12 | R | 100
12 | R | 101
12 | S | 220
13 | D | 88
14 | K | 151
14 | K | 152
我想要获取的是同一行上每个 objectID 的值的分组,以便每个 objectID 仅有一行。用图形术语来说:
objectID | value1a | value2a | value 1b | value2b | value1c | value2c
______________________________________________________________________________
12 | R | 100 | R | 101 | S | 220
13 | D | 88 | | | |
14 | K | 151 | K | 152 | |
空白单元格是空白的。
我一直希望在没有 VB 的 Excel 或 Access 中执行此操作,但是 CONCAT 和其他类似函数(以及此处和其他地方建议类似方法的响应)不起作用,因为每个值都需要保留在自己的单元格中(该数据最终将与 Word 形式合并)。如果答案是 SQL 存储过程或游标,那也没关系,尽管我现在编写它们的效率还不是很高。
感谢大家。
I'm trying to combine partial contents of rows that are the result set of a query from SQL Server 2005 that reads a .CSV. Here's a simplified version of the data I have:
objectID | value1 | value2
_________________________________
12 | R | 100
12 | R | 101
12 | S | 220
13 | D | 88
14 | K | 151
14 | K | 152
What I'm trying to get to is a grouping of each objectID's values on the same row, so that there is one and only one row for each objectID. In graphical terms:
objectID | value1a | value2a | value 1b | value2b | value1c | value2c
______________________________________________________________________________
12 | R | 100 | R | 101 | S | 220
13 | D | 88 | | | |
14 | K | 151 | K | 152 | |
Blank cells are blank.
I've been hoping to do this in Excel or Access without VB, but CONCAT and other similar functions (and responses here and elsewhere suggesting similar approaches) don't work because each value needs to stay in its own cell (this data will eventually be merged with a Word form). If the answer's a SQL stored procedure or cursor, that's okay, though I'm not terribly efficient at writing them just yet.
Thanks to all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先将数据导入到临时表中。临时表最终将得到类似于以下示例数据的内容:
然后,您可以使用此 SQL 批处理 - 如果需要,可以将其放入存储过程中。
输出
First import the data into a temp table. The temp table will end up something like this sample data:
Then, you can use this SQL batch - which can be put into a Stored Procedure if required.
Output