如何将来自不同列的值分组为一行?

时间:2013-02-05 19:23:12

标签: java sql sql-server pivot unpivot

我有一个如下所示的源表:

enter image description here

如何使结果表看起来像这样。我可以在sql或java代码中使用解决方案。

enter image description here

拜托,任何人都可以帮助我。 感谢。

2 个答案:

答案 0 :(得分:10)

我认为最简单的方法是同时应用UNPIVOTPIVOT函数来获得结果:

select account, description,
  [value_1], [value_2], [value_3], [value_4]
from
(
  select account, description, col, value,
    row_number() over(partition by account, col order by col) rn
  from 
  (
    select [account], [description], [value_1], [value_2], [value_3], [value_4]
    from yourtable
  ) src
  unpivot
  (
    value 
    for col in ([value_1], [value_2], [value_3], [value_4])
  ) un
) s
pivot
(
  max(value)
  for col in ([value_1], [value_2], [value_3], [value_4])
) piv

请参阅SQL Fiddle with Demo

也可以使用UNION ALL作为 unpivot 以及聚合函数和CASE表达式来完成此操作:

select account, description,
  max(case when col = 'value_1' then value end) value_1,
  max(case when col = 'value_2' then value end) value_2,
  max(case when col = 'value_3' then value end) value_3,
  max(case when col = 'value_4' then value end) value_4
from
(
  select account, description, col, value,
    row_number() over(partition by account, col order by account) rn
  from
  (
    select [account], [description], 'value_1' col, [value_1] value 
    from yourtable
    where [value_1] is not null
    union all
    select [account], [description], 'value_2' col, [value_2] value 
    from yourtable
    where [value_2] is not null
    union all
    select [account], [description], 'value_3' col, [value_3] value 
    from yourtable
    where [value_3] is not null
    union all
    select [account], [description], 'value_4' col, [value_4] value 
    from yourtable
    where [value_4] is not null
  ) s
) un
group by account, description, rn

请参阅SQL Fiddle with Demo

两者都给出结果:

| ACCOUNT |  DESCRIPTION |  VALUE_1 |  VALUE_2 |  VALUE_3 |  VALUE_4 |
----------------------------------------------------------------------
|  A00005 | Account Desc | ABCD0081 | BCDE0010 | BKCP0010 | SMTP0010 |
|  A00005 | Account Desc | ABCD0082 |   (null) | BKCP0011 |   (null) |

答案 1 :(得分:0)

一个想法是使用Row_Number()作为您的加入字段。像这样:

SELECT T.Account, T.Description, T1.Value_1, T2.Value_2, T3.Value_3, T4.Value_4
From 
  (
      SELECT Account, Description, Value_1, Value_2, Value_3, Value_4, 
           Row_number() OVER (ORDER BY (SELECT 0)) rn
      FROM YourTable
  ) T
  LEFT JOIN (
        SELECT Account, Value_1, Row_number() OVER (ORDER BY (SELECT 0)) rn
        FROM YourTable
        WHERE Value_1 <> '') T1 ON T.Account = T1.Account AND T.rn = T1.rn
  LEFT JOIN (
        SELECT Account, Value_2, Row_number() OVER (ORDER BY (SELECT 0)) rn
        FROM YourTable 
        WHERE Value_2 <> ''
  ) T2 ON T.Account = T2.Account AND T.rn = T2.rn
  LEFT JOIN (
        SELECT Account, Value_3, Row_number() OVER (ORDER BY (SELECT 0)) rn 
        FROM YourTable 
        WHERE Value_3 <> ''
  ) T3 ON T.Account = T3.Account AND T.rn = T3.rn
  LEFT JOIN (
        SELECT Account, Value_4, Row_number() OVER (ORDER BY (SELECT 0)) rn 
        FROM YourTable 
        WHERE Value_4 <> ''
  ) T4 ON T.Account = T4.Account AND T.rn = T4.rn
WHERE T1.Value_1 IS NOT NULL 
  OR T2.VALUE_2 IS NOT NULL 
  OR T3.VALUE_3 IS NOT NULL 
  OR T4.VALUE_4 IS NOT NULL 

如果您的值存储为空白,那么这应该可行。如果它们存储为NULL,请替换&lt;&gt; ''与IS NOT NULL。

这是SQL Fiddle

祝你好运。