以下是我正在使用的查询 - 我需要获取每个PhaseEndDt的MAX日期。我试过了
(SELECT Max(v) FROM (VALUES (aphase1.updated_ts), (aphase2.updated_ts), (aphase3.updated_ts)) AS VALUE(v)) AS [MaxDate]
但它不起作用:-(任何帮助都将非常感谢!
SELECT
dc.case_id,
aphase1.identifier_value AS "phase1",
aphase1.updated_ts AS "phase1_enddt",
aphase2.identifier_value AS "phase2",
aphase2.updated_ts AS "phase2_enddt",
aphase3.identifier_value AS "phase3",
APHASE3.UPDATED_TS AS "Phase3_EndDt"
FROM cmreporting.d_solution ds
INNER JOIN cmreporting.d_case dc ON ds.solution_sqn = dc.solution_sqn
LEFT JOIN cmreporting.a_identifiers aphase1 ON aphase1.identifier_value = 'Phase 1' AND dc.case_ID = aphase1.group_ID
LEFT JOIN cmreporting.a_identifiers aphase2 ON aphase2.identifier_value = 'Phase 2' AND dc.case_ID = aphase2.group_ID
LEFT JOIN cmreporting.a_identifiers aphase3 ON aphase3.identifier_value = 'Phase 3' AND dc.case_ID = aphase3.group_ID
答案 0 :(得分:0)
如果您只需要max_ updated_ts,则可以使用子查询或大小写:
SELECT
dc.case_id,
aphase1.identifier_value AS "phase1",
aphase1.updated_ts AS "phase1_enddt",
aphase2.identifier_value AS "phase2",
aphase2.updated_ts AS "phase2_enddt",
aphase3.identifier_value AS "phase3",
aphase3.updated_ts AS "phase3_enddt",
(SELECT MAX(updated_ts) FROM cmreporting.a_identifiers WHERE group_ID = dc.case_ID AND identifier_value IN('Phase 1', 'Phase 2', 'Phase 3')) AS max_updated_ts_subquery,
CASE WHEN aphase1.updated_ts > aphase2.updated_ts AND aphase1.updated_ts > aphase3.updated_ts THEN aphase1.updated
WHEN aphase2.updated_ts > aphase3.updated_ts THEN aphase2.updated_ts
ELSE aphase3.updated_ts END AS max_updated_ts_case,
(SELECT MAX(col) FROM (VALUES(aphase1.updated_ts), (aphase2.updated_ts), (aphase3.updated_ts)) AS tab(col)) AS max_updated_ts_from_values
FROM cmreporting.d_solution ds
INNER JOIN cmreporting.d_case dc ON ds.solution_sqn = dc.solution_sqn
LEFT JOIN cmreporting.a_identifiers aphase1 ON aphase1.identifier_value = 'Phase 1' AND dc.case_ID = aphase1.group_ID
LEFT JOIN cmreporting.a_identifiers aphase2 ON aphase2.identifier_value = 'Phase 2' AND dc.case_ID = aphase2.group_ID
LEFT JOIN cmreporting.a_identifiers aphase3 ON aphase3.identifier_value = 'Phase 3' AND dc.case_ID = aphase3.group_ID
编辑: 但是对于oracle使用GREATEST 像这样:
SELECT
dc.case_id,
aphase1.identifier_value AS "phase1",
aphase1.updated_ts AS "phase1_enddt",
aphase2.identifier_value AS "phase2",
aphase2.updated_ts AS "phase2_enddt",
aphase3.identifier_value AS "phase3",
aphase3.updated_ts AS "phase3_enddt",
GREATEST(aphase1.updated_ts, aphase2.updated_ts, aphase3.updated_ts) AS MaxDate
FROM cmreporting.d_solution ds
INNER JOIN cmreporting.d_case dc ON ds.solution_sqn = dc.solution_sqn
LEFT JOIN cmreporting.a_identifiers aphase1 ON aphase1.identifier_value = 'Phase 1' AND dc.case_ID = aphase1.group_ID
LEFT JOIN cmreporting.a_identifiers aphase2 ON aphase2.identifier_value = 'Phase 2' AND dc.case_ID = aphase2.group_ID
LEFT JOIN cmreporting.a_identifiers aphase3 ON aphase3.identifier_value = 'Phase 3' AND dc.case_ID = aphase3.group_ID
答案 1 :(得分:0)
万一其他人有同样的问题,这就是我终于能够开始工作了!
WITH CTE_Result AS(
SELECT
DC.CASE_ID,
APHASE1.IDENTIFIER_VALUE AS "Phase1",
APHASE1.UPDATED_TS AS "Phase1Enddt",
APHASE2.identifier_value AS "Phase2",
APHASE2.UPDATED_TS AS "Phase2EndDt",
APHASE3.identifier_value AS "Phase3",
APHASE3.UPDATED_TS AS "Phase3EndDt",
ROW_NUMBER() OVER(PARTITION BY DC.CASE_ID ORDER BY APHASE1.UPDATED_TS DESC, APHASE2.UPDATED_TS DESC, APHASE3.UPDATED_TS DESC) AS Row_id
FROM CMREPORTING.D_SOLUTION DS
INNER JOIN CMREPORTING.D_CASE DC ON DS.SOLUTION_SQN = DC.SOLUTION_SQN
LEFT OUTER JOIN CMREPORTING.A_IDENTIFIERS APHASE1 on APHASE1.identifier_value = 'Phase 1' and dc.case_ID = APHASE1.group_ID
LEFT OUTER JOIN CMREPORTING.A_IDENTIFIERS APHASE2 on APHASE2.identifier_value = 'Phase 2' and dc.case_ID = APHASE2.group_ID
LEFT OUTER JOIN CMREPORTING.A_IDENTIFIERS APHASE3 on APHASE3.identifier_value = 'Phase 3' and dc.case_ID = APHASE3.group_ID
WHERE
DC.CASE_ID = '{091225F8-4606-401C-872E-FC5ACDC1D8E2}'
)
SELECT *FROM CTE_Result WHERE Row_id=1
答案 2 :(得分:0)
你可以更容易地做到,也可能更快:
WITH
parms as (select 'Phase 1' AS "Phase1", 'Phase 2' AS "Phase2", 'Phase 3' AS "Phase3", '{091225F8-4606-401C-872E-FC5ACDC1D8E2}' AS case_id from dual)
SELECT
dc.case_id,
parms."Phase1",
SELECT Max(updated_ts) FROM a_identifiers WHERE identifier_value = parms."Phase1" AND group_ID = dc.case_ID) AS "Phase1Enddt",
parms."Phase2",
SELECT Max(updated_ts) FROM a_identifiers WHERE identifier_value = parms."Phase2" AND group_ID = dc.case_ID) AS "Phase2Enddt",
parms."Phase3",
SELECT Max(updated_ts) FROM a_identifiers WHERE identifier_value = parms."Phase3" AND group_ID = dc.case_ID) AS "Phase3Enddt",
FROM parms, cmreporting.d_solution ds
INNER JOIN cmreporting.d_case dc ON ds.solution_sqn = dc.solution_sqn
WHERE dc.case_id = parms.case_id
AND rownum = 1 --if ther is more then one row
编辑:
您可以通过此查询获得相同的结果:
WITH
parms as (select 'Phase 1' AS "Phase1", 'Phase 2' AS "Phase2", 'Phase 3' AS "Phase3", '{091225F8-4606-401C-872E-FC5ACDC1D8E2}' AS case_id from dual)
SELECT
group_ID as case_id,
parms."Phase1",
Max(Case When identifier_value = parms."Phase1" Then updated_ts End) AS "Phase1Enddt",
parms."Phase2",
Max(Case When identifier_value = parms."Phase2" Then updated_ts End) AS "Phase2Enddt",
parms."Phase3",
Max(Case When identifier_value = parms."Phase3" Then updated_ts End) AS "Phase3Enddt",
FROM parms, a_identifiers
Where Exists (Select 1 From cmreporting.d_solution ds
INNER JOIN cmreporting.d_case dc ON ds.solution_sqn = dc.solution_sqn
WHERE dc.case_id = a_identifiers.group_ID)
AND group_ID = parms.case_ID
GROUP BY group_ID