如何获得已解码/未透视行的唯一计数(列到行)

时间:2019-04-02 15:47:57

标签: database oracle unpivot

使用Oracle DB。
将一组列转换为行并为每行获取唯一计数时遇到问题。

我正在使用一个表,该表标识附加到特定帐号的一组值(最多六个)。我想取消对这六列的透视,以便它们在自己的行中列出。每行都有一个代表位置值的计数(即值1 = 1,值3 = 3,依此类推)

例如,我有一个如下表。 每个帐号的值(1-6)始终是唯一的

AccountNumber|Value1|Value2|Value3|Value4|Value5|Value6
-------------------------------------------------------
1            |123   |1234  |12345 |12    |12345 |1234
2            |123   |1234  |12345 |12    |12345 |1234
3            |123   |1234  |12345 |12    |12345 |1234

我希望优先级顺序附加到值编号上。

AccountNumber|Order|Value
-------------------------
1            |1    |123
1            |2    |1234
1            |3    |12345
1            |4    |12
1            |5    |123456
1            |6    |1
2            |1    |123
2            |2    |1234
2            |3    |12345
2            |4    |12
2            |5    |123456
2            |6    |1
3            |1    |123
3            |2    |1234
3            |3    |12345
3            |4    |12
3            |5    |123456
3            |6    |1

我有以下内容

SELECT
'1' AS FACILITYID,
HL.ACCOUNT_ID AS AccountNumber,
--UNPIVOT_ROW,
DECODE(UNPIVOT_ROW, 1, TDL.DX_ONE_ID,
                    2, TDL.DX_TWO_ID,
                    3, TDL.DX_THREE_ID,
                    4, TDL.DX_FOUR_ID,
                    5, TDL.DX_FIVE_ID,
                    6, TDL.DX_SIX_ID) AS Value,

FROM ACCOUNT_LIST HL
INNER JOIN TRANSACTIONS TRAN ON HL.ACCOUNT_ID = TRAN.ACCOUNT_ID,
(SELECT LEVEL AS UNPIVOT_ROW FROM DUAL CONNECT BY LEVEL <= 6)

我能够取消枢纽操作(使用DECODE(),UNPIVOT()给了我一些问题),但我似乎无法将头数与唯一计数相关联。

这可能很容易,但是我想知道一种有效的方法来处理20,000多个行,然后再进行无太多开销的操作,这只是我脑海中浮出水面。

我尝试返回值UNPIVOT_ROW并使用第二个解码来输出数字,但它一直吐出数字'1'

任何帮助或建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

我认为您过于复杂了,只需要基本的基本操作即可:

select account_id as "AccountNumber", position as "Order", value as "Value" 
from (
  select 1 as facility_id, hl.account_id, tdl.dx_one_id, tdl.dx_two_id,
    tdl.dx_three_id, tdl.dx_four_id, tdl.dx_five_id, tdl.dx_six_id
  from account_list hl
  inner join transactions tdl on hl.account_id = tdl.account_id
)
unpivot (value for position in (dx_one_id as 1, dx_two_id as 2, dx_three_id as 3,
                                dx_four_id as 4, dx_five_id as 5, dx_six_id as 6))

演示的起始数据与您的第一个示例匹配:

-- CTEs for sample data
with account_list (account_id) as (
            select 1 from dual
  union all select 2 from dual
  union all select 3 from dual
),
transactions (account_id, dx_one_id, dx_two_id, dx_three_id, dx_four_id, dx_five_id, dx_six_id) as (
            select 1, 123, 1234, 12345, 12, 12345, 1234 from dual
  union all select 2, 123, 1234, 12345, 12, 12345, 1234 from dual
  union all select 3, 123, 1234, 12345, 12, 12345, 1234 from dual
)
-- actual query
select account_id as "AccountNumber", position as "Order", value as "Value" 
from (
  select 1 as facility_id, hl.account_id, tdl.dx_one_id, tdl.dx_two_id,
    tdl.dx_three_id, tdl.dx_four_id, tdl.dx_five_id, tdl.dx_six_id
  from account_list hl
  inner join transactions tdl on hl.account_id = tdl.account_id
)
unpivot (value for position in (dx_one_id as 1, dx_two_id as 2, dx_three_id as 3,
                                dx_four_id as 4, dx_five_id as 5, dx_six_id as 6))
order by facility_id, account_id, position;

得到

AccountNumber      Order      Value
------------- ---------- ----------
            1          1        123
            1          2       1234
            1          3      12345
            1          4         12
            1          5      12345
            1          6       1234
            2          1        123
            2          2       1234
            2          3      12345
            2          4         12
            2          5      12345
            2          6       1234
            3          1        123
            3          2       1234
            3          3      12345
            3          4         12
            3          5      12345
            3          6       1234

18 rows selected.