1

我在数据库中有一个表客户,如下所示。

ID  UID  Address1  Name  code
10  5    A         Jac   683501
11  5    B         Joe   727272
13  6    C         mat   373737

前两条记录 (10,11) 有一个共同的 uID -5 。这两个记录可以被视为一个单元。第四条记录有一个单独的 UID ,所以它是一个单独的单元

我需要在 csv 文件中生成一个输出,这样

ID  UID Name  code     Address1  Name2  Code2
10  5   jac   683501   A         Joe    727272 
13  6   mat   373737   C 

Name2 和 code2 值来自第二行,因为前两条记录的 UID 相同,我们可以将其视为一个单元。

任何人都可以提供查询以生成这些记录的提示。

4

2 回答 2

2

将数据从行转换为列的过程称为 PIVOT。有几种方法可以做到这一点。

您可以将 arow_number()与带有 CASE 表达式的聚合函数一起使用:

select min(id),
  uid,
  max(case when seq = 1 then name end) Name,
  max(case when seq = 1 then code end) Code,
  max(case when seq = 1 then Address1 end) Address1,
  max(case when seq = 2 then name end) Name2,
  max(case when seq = 2 then code end) code2,
  max(case when seq = 2 then Address1 end) Address1_2
from
(
  select id, uid, address1, name, code,
    row_number() over(partition by uid order by id) seq
  from yourtable
) d
group by uid;

请参阅SQL Fiddle with Demo

您可以同时使用 UNPIVOT 和 PIVOT 函数:

select id, uid,
  name1, code1, address1, name2, code2, address2
from 
(
  select id, uid, col+cast(seq as varchar(10)) col, value
  from
  (
    select 
      (select min(id) 
       from yourtable t2
       where t.uid = t2.uid) id, 
      uid, 
      cast(address1 as varchar(20)) address, 
      cast(name as varchar(20)) name, 
      cast(code as varchar(20)) code,
      row_number() over(partition by uid order by id) seq
    from yourtable t
  ) d
  unpivot
  (
    value
    for col in (address, name, code)
  ) unpiv
) src
pivot
(
  max(value)
  for col in (name1, code1, address1, name2, code2, address2)
) piv;

请参阅SQL Fiddle with Demo

最后,如果每个 都有未知数量的值uid,则可以使用动态 SQL 来获取结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(col+(cast(seq as varchar(10)))) 
                    from
                    (
                      select row_number() over(partition by uid order by id) seq
                      from yourtable
                    ) d
                    cross apply
                    (
                      select 'name', 1 union all
                      select 'code', 2 union all
                      select 'address', 3 
                    ) c (col, so)
                    group by seq, col, so
                    order by seq, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT id, uid,' + @cols + ' 
            from 
            (
                select id, uid, col+cast(seq as varchar(10)) col, value
                from
                (
                  select 
                    (select min(id) 
                     from yourtable t2
                     where t.uid = t2.uid) id, 
                    uid, 
                    cast(address1 as varchar(20)) address, 
                    cast(name as varchar(20)) name, 
                    cast(code as varchar(20)) code,
                    row_number() over(partition by uid order by id) seq
                  from yourtable t
                ) d
                unpivot
                (
                  value
                  for col in (address, name, code)
                ) unpiv
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute(@query);

请参阅SQL Fiddle with Demo。所有版本都会给出一个结果:

| ID | UID | NAME1 |  CODE1 | ADDRESS1 |  NAME2 |  CODE2 | ADDRESS2 |
---------------------------------------------------------------------
| 10 |   5 |   Jac | 683501 |        A |    Joe | 727272 |        B |
| 13 |   6 |   mat | 373737 |        C | (null) | (null) |   (null) |
于 2013-06-24T12:05:06.780 回答
1

尝试内连接。

select u2.firstname,u2.col2,
from users u
inner join users u2 on u.userid=u2.userid
where u.firstname=u2.lastname
于 2013-06-24T11:29:42.587 回答