如何在另一列中显示最大日期金额?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在另一列中显示最大日期金额?相关的知识,希望对你有一定的参考价值。
我想根据最新日期记录ADULT_AMT将MSP_ADULT和MSP_CHILD列驱动在MSP_ADULT中,将CHILD_AMT驱动在MSP_CHILD列中。
我想像下面这样出去。
END_DATE ADULT_AMT CHILD_AMT MSP_ADULT MSP_CHILD
09/01/2017 100 50 180 80
10/01/2018 200 100 180 80
04/05/2019 300 90 180 80
08/20/2019 180 80 180 80
这是我正在运行的代码,但不起作用。
SELECT
AL1.END_DATE as BROCHURE_EFFECTIVE_END_DATE,
PORT_CODE,
AL7.PRODUCT_LEGACY_CODE as COMPONENT_CODE,
AL3.PRICE_GUEST_AGE_GROUP ,
MAX(CASE WHEN AL3.PRICE_GUEST_AGE_GROUP = 'ADULT' THEN AL3.PRICE_AMOUNT ELSE 0 END) ADULT_AMT,
MAX(CASE WHEN AL3.PRICE_GUEST_AGE_GROUP = 'CHILD' THEN AL3.PRICE_AMOUNT ELSE 0 END) CHILD_AMT,
MAX(CASE WHEN AL3.PRICE_GUEST_AGE_GROUP = 'ADULT' THEN AL3.PRICE_AMOUNT ELSE 0 END)
OVER (PARTITION BY PORT_CODE, AL7.PRODUCT_LEGACY_CODE --order by AL1.END_DATE desc
)AS MSP_ADULT,
MAX(CASE WHEN AL3.PRICE_GUEST_AGE_GROUP = 'CHILD' THEN AL3.PRICE_AMOUNT ELSE 0 END)
OVER (PARTITION BY PORT_CODE, AL7.PRODUCT_LEGACY_CODE order by AL1.END_DATE desc ) AS MSP_child
FROM RATE_PLAN AL1
inner join PRICE AL3
on (AL3.RATE_PLAN_SK=AL1.RATE_PLAN_SK and AL3.rate_plan_sk <>-1 )
Inner join PRODUCT_VARIANT AL7
ON (AL3.PRODUCT_CODE = AL7.PRODUCT_LEGACY_CODE and AL7.CATALOG_VERSION='Online')
INNER JOIN PRODUCT_OFFERING AL14
ON (AL14.PRODUCT_CODE = AL7.PRODUCT_LEGACY_CODE and AL7.CATALOG_VERSION='Online')
inner join PORT
on (AL7.FULFILLMENT_LOCATION = PORT_CODE)
where TO_CHAR(AL1.END_DATE,'YYYY') >= TO_CHAR(SYSDATE,'YYYY')
and AL3.use_for_pricing_flag is not null
and port_code='HKT' and AL7.product_LEGACY_code='PK83'
GROUP BY
PORT_CODE,
PRICE_GUEST_AGE_GROUP,
AL7.PRODUCT_LEGACY_CODE,
AL1.END_DATE
)
答案
因此,您需要这些条件MAX的最后一个值。
尝试按降序排列FIRST_VALUE。
...
FIRST_VALUE(ADULT_AMT)
OVER (PARTITION BY PORT_CODE, COMPONENT_CODE ORDER BY BROCHURE_EFFECTIVE_END_DATE DESC) AS MSP_ADULT,
FIRST_VALUE(CHILD_AMT)
OVER (PARTITION BY PORT_CODE, COMPONENT_CODE ORDER BY BROCHURE_EFFECTIVE_END_DATE DESC) AS MSP_CHILD
...
请注意,还有LAST_VALUE窗口功能,但有时可能会产生误导。
以上是关于如何在另一列中显示最大日期金额?的主要内容,如果未能解决你的问题,请参考以下文章
clickhouse:如何在另一列中找到高于日期的数组中的最小日期?