我有一点看起来像这样的SQL。
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET PRINCIPAL = CASE
WHEN TPOS.PID = 'O.D.7' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8' THEN TPOS.PRINAMT
END,
INTEREST = CASE
WHEN TPOS.PID = 'O.O.19' THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6' THEN (TPOS.QTY * SELECT(MAX(SECS.INTPAYAMT_8)))
END,
INTPAYAMTQTY = CASE
WHEN TPOS.PID = 'O.W.16' THEN TPOS.MKTVAL + (TPOS.QTY*SECS.INTPAYAMT_8)
END,
REPORTABLE_AMOUNT = CASE
WHEN TPOS.PID = 'O.D.7' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8' THEN TPOS.PRINAMT
WHEN TPOS.PID = 'O.O.19' THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6' THEN (TPOS.QTY * SELECT(MAX(SECS.INTPAYAMT_8)))
WHEN TPOS.PID = 'O.W.16' THEN TPOS.MKTVAL + (TPOS.QTY*SECS.INTPAYAMT_8)
END
FROM TBL_FR2052A_RAW_DATA_HIST AS RawData
INNER JOIN TBL_FR2052A_SECS_HIST AS SECS
INNER JOIN TBL_FR2052A_TPOS_HIST_SPLIT AS TPOS ON SECS.SECID = TPOS.SECID
AND SECS.ASOFDATE = TPOS.ASOFDATE
ON RawData.SourceID_Revised = TPOS.SourceID_Revised
AND RawData.AsofDate = TPOS.ASOFDATE
现在,我正试图获得SECS.INTPAYAMT_8
我认为会是这样的:
WHEN TPOS.PID = 'I.O.6' THEN (TPOS.QTY * MAX(SECS.INTPAYAMT_8)) END,
当然,这不起作用。我在线查看,它说你必须使用子查询。我尝试了一些想法,但到目前为止没有任何效果。
这里有专家知道怎么做这种事吗? TIA。
答案 0 :(得分:1)
在加入SECS之前,首先计算MAX(SECS.INTPAYAMT_8)。
所以代码如下所示(您需要根据您的架构优化代码):
UPDATE TBL_FR2052A_TPOS_HIST_SPLIT
SET PRINCIPAL = CASE WHEN TPOS.PID = 'O.D.7' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8' THEN TPOS.PRINAMT END,
INTEREST = CASE WHEN TPOS.PID = 'O.O.19' THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6' THEN (TPOS.QTY * MAX_INTPAYAMT_8) END,
INTPAYAMTQTY = CASE WHEN TPOS.PID = 'O.W.16' THEN TPOS.MKTVAL + (TPOS.QTY*SECS.INTPAYAMT_8) END,
REPORTABLE_AMOUNT = CASE WHEN TPOS.PID = 'O.D.7' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1' THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8' THEN TPOS.PRINAMT
WHEN TPOS.PID = 'O.O.19' THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6' THEN (TPOS.QTY * MAX_INTPAYAMT_8)
WHEN TPOS.PID = 'O.W.16' THEN TPOS.MKTVAL + (TPOS.QTY*SECS.INTPAYAMT_8)END
FROM TBL_FR2052A_RAW_DATA_HIST AS RawData INNER JOIN
(
select *,
-- Calculate out MAX_INTPAYAMT_8
max(INTPAYAMT_8) over (partition by ID) MAX_INTPAYAMT_8 --I do not know your schema, so use partiion by ID
from TBL_FR2052A_SECS_HIST
)
AS SECS INNER JOIN TBL_FR2052A_TPOS_HIST_SPLIT AS TPOS ON SECS.SECID = TPOS.SECID AND
SECS.ASOFDATE = TPOS.ASOFDATE ON RawData.SourceID_Revised = TPOS.SourceID_Revised AND
RawData.AsofDate = TPOS.ASOFDATE
答案 1 :(得分:0)
或者,在单独的子查询中获取INTPAYAMT_8的最大值,然后将其加入主查询。
;WITH CTE
AS (
SELECT CASE
WHEN TPOS.PID = 'O.D.7'
THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1'
THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8'
THEN TPOS.PRINAMT
END PRINCIPAL_, CASE
WHEN TPOS.PID = 'O.O.19'
THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6'
THEN (TPOS.QTY * T.INTPAYAMT_8)
END INTEREST_, CASE
WHEN TPOS.PID = 'O.W.16'
THEN TPOS.MKTVAL + (TPOS.QTY * SECS.INTPAYAMT_8)
END INTPAYAMTQTY_, CASE
WHEN TPOS.PID = 'O.D.7'
THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.A.1'
THEN TPOS.MKTVAL
WHEN TPOS.PID = 'I.O.8'
THEN TPOS.PRINAMT
WHEN TPOS.PID = 'O.O.19'
THEN TPOS.QTY * SECS.INTPAYAMT_8
WHEN TPOS.PID = 'I.O.6'
THEN (TPOS.QTY * T.INTPAYAMT_8)
WHEN TPOS.PID = 'O.W.16'
THEN (TPOS.MKTVAL + (TPOS.QTY * SECS.INTPAYAMT_8))
END REPORTABLE_AMOUNT_, PRINCIPAL, INTEREST, INTPAYAMTQTY, REPORTABLE_AMOUNT
FROM TBL_FR2052A_RAW_DATA_HIST AS RawData
INNER JOIN TBL_FR2052A_TPOS_HIST_SPLIT AS TPOS ON (
RawData.SourceID_Revised = TPOS.SourceID_Revised
AND RawData.AsofDate = TPOS.ASOFDATE
)
INNER JOIN TBL_FR2052A_SECS_HIST AS SECS ON (
SECS.SECID = TPOS.SECID
AND SECS.ASOFDATE = TPOS.ASOFDATE
)
LEFT OUTER JOIN (
SELECT RawData.SourceID_Revised, TPOS.ASOFDATE, MAX(SECS.INTPAYAMT_8) INTPAYAMT_8
FROM TBL_FR2052A_RAW_DATA_HIST AS RawData
INNER JOIN TBL_FR2052A_TPOS_HIST_SPLIT AS TPOS ON (
RawData.SourceID_Revised = TPOS.SourceID_Revised
AND RawData.AsofDate = TPOS.ASOFDATE
)
INNER JOIN TBL_FR2052A_SECS_HIST AS SECS ON (
SECS.SECID = TPOS.SECID
AND SECS.ASOFDATE = TPOS.ASOFDATE
)
GROUP BY RawData.SourceID_Revised, TPOS.ASOFDATE
) T ON RawData.SourceID_Revised = T.SourceID_Revised
AND RawData.AsofDate = T.ASOFDATE
)
UPDATE CTE
SET PRINCIPAL = PRINCIPAL_, INTEREST = INTEREST_, INTPAYAMTQTY = INTPAYAMTQTY_, REPORTABLE_AMOUNT = REPORTABLE_AMOUNT_