Oracle-如何从多个相同类型的行中获取单行
Oracle- How to get single row from multiple same type of rows
我有以下 table 个查询输出。
并且我希望从上面的输出中得到如下图所示的最终输出。主要目标是根据 reported_on 和 created_on 日期计算出每一年每个 ffi_customer_id 的一行。
提前致谢。
这类问题可以用解析函数来解决。您可以在此处阅读更多内容 https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174
例如,您 select 您想要在外部 select 中的列,然后在子查询中按 ffi_customer_id、ref_period_value_code 顺序排列分区 created_on 和 reported_on 降序排列。然后select排名为1的记录
SELECT ffi_customer_id
,ref_period_value_code
,created_on
,reported_on
,financial_master_id
FROM ( SELECT your_table_name.*
,RANK() OVER(PARTITION BY ffi_customer_id, ref_period_value_code ORDER BY reported_on DESC, created_on DESC) AS "Rank"
FROM (SELECT * FROM your_table_name) AS table2(ffi_customer_id, ref_period_value_code, created_on, reported_on, financial_master_id)) table22) t0 WHERE "Rank" = 1;
我有以下 table 个查询输出。
并且我希望从上面的输出中得到如下图所示的最终输出。主要目标是根据 reported_on 和 created_on 日期计算出每一年每个 ffi_customer_id 的一行。
提前致谢。
这类问题可以用解析函数来解决。您可以在此处阅读更多内容 https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174
例如,您 select 您想要在外部 select 中的列,然后在子查询中按 ffi_customer_id、ref_period_value_code 顺序排列分区 created_on 和 reported_on 降序排列。然后select排名为1的记录
SELECT ffi_customer_id
,ref_period_value_code
,created_on
,reported_on
,financial_master_id
FROM ( SELECT your_table_name.*
,RANK() OVER(PARTITION BY ffi_customer_id, ref_period_value_code ORDER BY reported_on DESC, created_on DESC) AS "Rank"
FROM (SELECT * FROM your_table_name) AS table2(ffi_customer_id, ref_period_value_code, created_on, reported_on, financial_master_id)) table22) t0 WHERE "Rank" = 1;