3

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

在此处输入图像描述

如何制作结果表如下所示。我可以在 sql 或 java 代码中使用解决方案。

在此处输入图像描述

拜托,谁能帮帮我。谢谢。

4

2 回答 2

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

这也可以使用 aUNION 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

两者都给出结果:

| ACCOUNT |  DESCRIPTION |  VALUE_1 |  VALUE_2 |  VALUE_3 |  VALUE_4 |
----------------------------------------------------------------------
|  A00005 | Account Desc | ABCD0081 | BCDE0010 | BKCP0010 | SMTP0010 |
|  A00005 | Account Desc | ABCD0082 |   (null) | BKCP0011 |   (null) |
于 2013-02-05T19:40:43.607 回答
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,请将 <> '' 替换为 IS NOT NULL。

这是一个SQL Fiddle

祝你好运。

于 2013-02-05T19:32:56.637 回答