TASKS DATE
NOD 1 OUT 2/5/10
NOD 1 IN NULL
NOD 2 OUT 2/10/10
NOD 2 IN 2/11/10
NOD 3 OUT 2/15/10
NOD 3 IN NULL
我需要做的是找到最大的NOD,然后如果NOD有一个IN日期,在行中放置'NO NOD',但是如果最大的NOD已经发送出去但没有收到回来IN,计算天数既然已经发出去了。我试图嵌套 case 语句,但发生的是它看到 NOD 2 OUT 然后 IN 并放置“No NOD”文本。我不知道如何让它找到最大的点头。这是我到目前为止所拥有的(我是 SQL 的新手,所以如果有什么问题我很抱歉,而且我知道我的格式不好,但我可以看到这种方式更好。):
CASE
WHEN COUNT(DISTINCT DATE_NOD_TBL.COMPLETED_DATE) = '0' THEN 'No NOD Out'
WHEN MAX(DATE_NOD_TBL_2.COMPLETED_DATE) IS NOT NULL AND MAX(DATE_NOD_TBL_5.COMPLETED_DATE) IS NOT NULL
THEN (CASE
WHEN MAX(DATE_NOD_TBL_3.COMPLETED_DATE) IS NOT NULL AND MAX(DATE_NOD_TBL_6.COMPLETED_DATE) IS NOT NULL
THEN (CASE
WHEN MAX(DATE_NOD_TBL_4.COMPLETED_DATE) IS NOT NULL AND MAX(DATE_NOD_TBL_7.COMPLETED_DATE) IS NOT NULL
THEN 'No NOD Out_3'
ELSE TO_CHAR(TRUNC(SYSDATE - (MAX(DATE_NOD_TBL.COMPLETED_DATE))))
END)
ELSE TO_CHAR(TRUNC(SYSDATE - (MAX(DATE_NOD_TBL.COMPLETED_DATE))))
END)
ELSE TO_CHAR(TRUNC(SYSDATE - (MAX(DATE_NOD_TBL.COMPLETED_DATE))))
END AS "Days Since Last NOD",
我为表格设置了别名,以便每个任务都触摸一次表格,如下所示:
DATE_NOD_TBL contains all OUT tasks
DATE_NOD_TBL_2 is NOD 1 OUT
DATE_NOD_TBL_3 is NOD 2 OUT
DATE_NOD_TBL_4 is NOD 3 OUT
DATE_NOD_TBL_5 is NOD 1 IN
DATE_NOD_TBL_6 is NOD 2 IN
DATE_NOD_TBL_7 is NOD 3 IN
我正在使用 10g Oracle SQL。
整个查询:
SELECT DSKMTB_ACTIVITY_TYPE.ACTIVITY_TYPE_LABEL AS "Project Type",
MAX(DATE_ISSUED_TBL_3.ASSIGNED_STAFF_ID) AS "Project Manager",
AGENCY_INTEREST_ALT.ALTERNATE_AI_ID AS "Farm ID #",
MTB_PARISH_COUNTY.PARISH_OR_COUNTY_DESC AS "County",
MAX(DISTINCT AGENCY_INTEREST.MASTER_AI_NAME) AS "Operation Name",
MAX(DATE_RECEIVED_TBL.COMPLETED_DATE) AS "Date Received",
MAX(DATE_NOD_TBL.COMPLETED_DATE) AS "NOD Date",
COUNT(DISTINCT DATE_NOD_TBL.COMPLETED_DATE) AS "# of NOD's",
--//days since last NOD
CASE
WHEN GREATEST(MAX(DATE_NOD_TBL_2.COMPLETED_DATE),
MAX(DATE_NOD_TBL_3.COMPLETED_DATE),
MAX(DATE_NOD_TBL_4.COMPLETED_DATE)) <
GREATEST(MAX(DATE_NOD_TBL_5.COMPLETED_DATE),
MAX(DATE_NOD_TBL_6.COMPLETED_DATE),
MAX(DATE_NOD_TBL_7.COMPLETED_DATE))
THEN TO_CHAR(TRUNC(SYSDATE - (MAX(DATE_NOD_TBL.COMPLETED_DATE))))
ELSE 'No NOD Out'
END AS "Days Since Last NOD",
--//net time
CASE WHEN
COUNT(DISTINCT DATE_NOD_TBL.COMPLETED_DATE) = '0'
THEN TRUNC(MAX(SYSDATE - DATE_RECEIVED_TBL.COMPLETED_DATE))
ELSE
TRUNC(MAX((SYSDATE - DATE_RECEIVED_TBL.COMPLETED_DATE) -
--//issued NOD #1/NOD #1 Information Received
NVL(((CASE WHEN DATE_NOD_TBL_5.STATUS_CODE IS NULL THEN SYSDATE ELSE DATE_NOD_TBL_5.COMPLETED_DATE END) - DATE_NOD_TBL_2.COMPLETED_DATE),'0') -
--//issued NOD #2/NOD #2 Information Received
NVL(((CASE WHEN DATE_NOD_TBL_6.STATUS_CODE IS NULL THEN SYSDATE ELSE DATE_NOD_TBL_6.COMPLETED_DATE END) - DATE_NOD_TBL_3.COMPLETED_DATE),'0') -
--//issued NOD #3/NOD #3 Information Received
NVL(((CASE WHEN DATE_NOD_TBL_7.STATUS_CODE IS NULL THEN SYSDATE ELSE DATE_NOD_TBL_7.COMPLETED_DATE END) - DATE_NOD_TBL_4.COMPLETED_DATE),'0')
)) END
AS "Net Time",
TRUNC(MAX(SysDate - DATE_RECEIVED_TBL.COMPLETED_DATE)) AS "Gross Time",
MAX(DATE_SITEASSESSMENT_TBL.COMPLETED_DATE) AS "Site Visit"
FROM DSK_CENTRAL_FILE
INNER JOIN ACTIVITY_TASK_LIST DATE_ISSUED_TBL
ON DATE_ISSUED_TBL.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_ISSUED_TBL.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_ISSUED_TBL.REFERENCE_TASK_ID = '171'
AND DATE_ISSUED_TBL.STATUS_CODE IS NULL
INNER JOIN ACTIVITY_TASK_LIST DATE_ISSUED_TBL_2
ON DATE_ISSUED_TBL_2.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_ISSUED_TBL_2.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_ISSUED_TBL_2.REFERENCE_TASK_ID = '163'
AND DATE_ISSUED_TBL_2.STATUS_CODE IS NULL
INNER JOIN ACTIVITY_TASK_LIST DATE_ISSUED_TBL_3
ON DATE_ISSUED_TBL_3.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_ISSUED_TBL_3.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_ISSUED_TBL_3.REFERENCE_TASK_ID = '262'
INNER JOIN ACTIVITY_TASK_LIST DATE_RECEIVED_TBL
ON DATE_RECEIVED_TBL.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_RECEIVED_TBL.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_RECEIVED_TBL.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_RECEIVED_TBL.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_RECEIVED_TBL.REFERENCE_TASK_ID = '571'
AND DATE_RECEIVED_TBL.STATUS_CODE = '001'
AND DATE_RECEIVED_TBL.COMPLETED_DATE IS NOT NULL
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL
ON DATE_NOD_TBL.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL.REFERENCE_TASK_ID IN ( '393','394','395')
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_8
ON DATE_NOD_TBL_8.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_8.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_8.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_8.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_8.REFERENCE_TASK_ID IN('602','603','608')
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_2
ON DATE_NOD_TBL_2.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_2.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_2.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_2.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_2.REFERENCE_TASK_ID = '393'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_3
ON DATE_NOD_TBL_3.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_3.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_3.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_3.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_3.REFERENCE_TASK_ID = '394'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_4
ON DATE_NOD_TBL_4.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_4.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_4.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_4.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_4.REFERENCE_TASK_ID = '395'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_5
ON DATE_NOD_TBL_5.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_5.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_5.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_5.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_5.REFERENCE_TASK_ID = '603'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_6
ON DATE_NOD_TBL_6.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_6.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_6.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_6.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_6.REFERENCE_TASK_ID = '602'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_NOD_TBL_7
ON DATE_NOD_TBL_7.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_NOD_TBL_7.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_NOD_TBL_7.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_NOD_TBL_7.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_NOD_TBL_7.REFERENCE_TASK_ID = '608'
LEFT OUTER JOIN ACTIVITY_TASK_LIST DATE_SITEASSESSMENT_TBL
ON DATE_SITEASSESSMENT_TBL.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND DATE_SITEASSESSMENT_TBL.INT_DOC_ID = DSK_CENTRAL_FILE.INT_DOC_ID
AND DATE_SITEASSESSMENT_TBL.INT_DOC_ID = DATE_ISSUED_TBL.INT_DOC_ID
AND DATE_SITEASSESSMENT_TBL.MASTER_AI_ID = DATE_ISSUED_TBL.MASTER_AI_ID
AND DATE_SITEASSESSMENT_TBL.REFERENCE_TASK_ID = '201'
INNER JOIN DSKMTB_ACTIVITY_TYPE
ON DSKMTB_ACTIVITY_TYPE.ACTIVITY_CATEGORY_CODE = DSK_CENTRAL_FILE.ACTIVITY_CATEGORY_CODE
AND DSKMTB_ACTIVITY_TYPE.ACTIVITY_CLASS_CODE = DSK_CENTRAL_FILE.ACTIVITY_CLASS_CODE
AND DSKMTB_ACTIVITY_TYPE.ACTIVITY_TYPE_CODE = DSK_CENTRAL_FILE.ACTIVITY_TYPE_CODE
AND DSKMTB_ACTIVITY_TYPE.PROGRAM_CODE = DSK_CENTRAL_FILE.PROGRAM_CODE
AND DSKMTB_ACTIVITY_TYPE.PROGRAM_CODE = 'CF'
AND DSKMTB_ACTIVITY_TYPE.ACTIVITY_CATEGORY_CODE = 'ATH'
LEFT OUTER JOIN AGENCY_INTEREST_ALT
ON AGENCY_INTEREST_ALT.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND AGENCY_INTEREST_ALT.END_DATE IS NULL
AND AGENCY_INTEREST_ALT.USER_GROUP_ID = 'CFO'
INNER JOIN AGENCY_INTEREST
ON AGENCY_INTEREST.MASTER_AI_ID = AGENCY_INTEREST_ALT.MASTER_AI_ID
AND AGENCY_INTEREST.INT_DOC_ID = AGENCY_INTEREST_ALT.INT_DOC_ID
INNER JOIN SUBJ_ITEM_LOC_GOV_WITHIN
ON SUBJ_ITEM_LOC_GOV_WITHIN.MASTER_AI_ID = DSK_CENTRAL_FILE.MASTER_AI_ID
AND SUBJ_ITEM_LOC_GOV_WITHIN.INT_DOC_ID = AGENCY_INTEREST_ALT.INT_DOC_ID
INNER JOIN MTB_PARISH_COUNTY
ON MTB_PARISH_COUNTY.PARISH_OR_COUNTY_CODE = SUBJ_ITEM_LOC_GOV_WITHIN.PARISH_OR_COUNTY_CODE
WHERE DSK_CENTRAL_FILE.PROGRAM_CODE = 'CF'
AND DSK_CENTRAL_FILE.INT_DOC_ID != 0
AND AGENCY_INTEREST_ALT.INT_DOC_ID = 0
GROUP BY DSKMTB_ACTIVITY_TYPE.ACTIVITY_TYPE_LABEL,
AGENCY_INTEREST_ALT.ALTERNATE_AI_ID,
MTB_PARISH_COUNTY.PARISH_OR_COUNTY_DESC
ORDER BY DSKMTB_ACTIVITY_TYPE.ACTIVITY_TYPE_LABEL,
MAX(DATE_RECEIVED_TBL.COMPLETED_DATE)