简体   繁体   English

将列中的数据组合成单行

[英]Combining like data from columns into single row

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. 我正在尝试组合行的部分内容,这些行是SQL Server 2005中读取.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. 我想要的是在同一行上对每个objectID的值进行分组,以便每个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). 我一直希望在没有VB的Excel或Access中这样做,但是CONCAT和其他类似的功能(以及这里和其他地方提出类似方法的响应)不起作用,因为每个值都需要保留在自己的单元格中(这些数据最终会与Word表单合并)。 If the answer's a SQL stored procedure or cursor, that's okay, though I'm not terribly efficient at writing them just yet. 如果答案是SQL存储过程或游标,那没关系,尽管我还没有非常高效地编写它们。

Thanks to all. 谢谢大家。

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. 然后,您可以使用此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)

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM