将列中的类似数据合并到单行中

发布于 2024-10-25 10:20:17 字数 976 浏览 0 评论 0原文

我正在尝试合并作为读取 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2024-11-01 10:20:17

首先将数据导入到临时表中。临时表最终将得到类似于以下示例数据的内容:

create table #tmp (objectID int, value1 char(1), value2 int)
insert #tmp select
12 ,'R', 100 union all select
12 ,'R', 101 union all select
12 ,'S', 220 union all select
13 ,'D', 88 union all select
14 ,'K', 151 union all select
14 ,'K', 152

然后,您可以使用此 SQL 批处理 - 如果需要,可以将其放入存储过程中。

declare @sql nvarchar(max)
select @sql = ISNULL(@sql+',','')
        + 'max(case when rn=' + cast(number as varchar) + ' then value1 end) value' + cast(number as varchar) + 'a,'
        + 'max(case when rn=' + cast(number as varchar) + ' then value2 end) value' + cast(number as varchar) + 'b'
from master..spt_values
where type='P' and number between 1 and (
    select top 1 COUNT(*)
    from #tmp
    group by objectID
    order by 1 desc)

set @sql = '
    select objectID, ' + @sql + '
    from (
        select rn=ROW_NUMBER() over (partition by objectID order by value2), *
        from #tmp) p
    group by ObjectID'

exec (@sql)

输出

objectID    value1a value1b     value2a value2b     value3a value3b
----------- ------- ----------- ------- ----------- ------- -----------
12          R       100         R       101         S       220
13          D       88          NULL    NULL        NULL    NULL
14          K       151         K       152         NULL    NULL
Warning: Null value is eliminated by an aggregate or other SET operation.

First import the data into a temp table. The temp table will end up something like this sample data:

create table #tmp (objectID int, value1 char(1), value2 int)
insert #tmp select
12 ,'R', 100 union all select
12 ,'R', 101 union all select
12 ,'S', 220 union all select
13 ,'D', 88 union all select
14 ,'K', 151 union all select
14 ,'K', 152

Then, you can use this SQL batch - which can be put into a Stored Procedure if required.

declare @sql nvarchar(max)
select @sql = ISNULL(@sql+',','')
        + 'max(case when rn=' + cast(number as varchar) + ' then value1 end) value' + cast(number as varchar) + 'a,'
        + 'max(case when rn=' + cast(number as varchar) + ' then value2 end) value' + cast(number as varchar) + 'b'
from master..spt_values
where type='P' and number between 1 and (
    select top 1 COUNT(*)
    from #tmp
    group by objectID
    order by 1 desc)

set @sql = '
    select objectID, ' + @sql + '
    from (
        select rn=ROW_NUMBER() over (partition by objectID order by value2), *
        from #tmp) p
    group by ObjectID'

exec (@sql)

Output

objectID    value1a value1b     value2a value2b     value3a value3b
----------- ------- ----------- ------- ----------- ------- -----------
12          R       100         R       101         S       220
13          D       88          NULL    NULL        NULL    NULL
14          K       151         K       152         NULL    NULL
Warning: Null value is eliminated by an aggregate or other SET operation.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文