如何获得 decoded/unpivoted 行的唯一计数(从列到行)

How to get a unique count of decoded/unpivoted rows (column to row)

使用 Oracle 数据库。
将一组列转换为行并为每行获取唯一计数时出现问题。

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

例如,我有一个如下所示的 table。 每个帐号的值 (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”

如有任何帮助或建议,我们将不胜感激!

我认为你过于复杂了,只需要一个基本的逆轴操作:

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.