我是SQL的新手,我一直致力于我的First Query。现在我需要你的帮助。 我在Excel中有这个报告。我在AGE列N中计算AGE的公式。我一直在尝试创建一个SQL查询来提出相同的答案。到目前为止,我已经找到了我需要的列,并将所有数据提取到Match Excel。唯一的问题是我不知道如何以及在何处输入此IF语句,因此它将计算运行查询时的年龄。
Work Order Location MISC Status Actual Finish Finish Time Parent WO Work Type N Reported Date Reported Time Site Gen Age Sub Name Substation Location
1234567899 4074 COMP 11/5/14 3:08:49 PM CM 10/7/14 1:47:42 PM 29.05633102
12348574987 2946 SCHED CM 10/30/14 10:28:03 AM 188.5638542
AGE是一个公式,粘贴在下面:
=IF ((IF(Status<>"Comp",Today()-Reported date, actual finish-reported date))<0,0, IF(STATUS<>"COMP", TODAY()-REPORTED DATE, ACTUAL FINISH-REPORTED DATE))
SQL Writen查询
SELECT WO.WONUM,
LOCOFFDESC.DESCRIPTION AS OFFICE,
WO.STATUS,
WO.LOCATION,
CASE
WHEN ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) = 0)
THEN SUBSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2)), LENGTH(LOCOFF.EXT_LOC_HIERARCHY_PATH))
WHEN ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) IS NOT NULL)
THEN SUBSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2)), (INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) - (INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2))
END AS "SUBSTATION CASE",
WO.PARENT,
WO.WORKTYPE,
WO.REPORTEDBY,
WO.REPORTDATE,
WO.ACTFINISH,
WO.SITEID
FROM LOCATIONS LOCOFF
RIGHT JOIN MAXPRD.WORKORDER WO
ON WO.LOCATION = LOCOFF.LOCATION
INNER JOIN MAXPRD.LOCANCESTOR LOCOFFANC
ON LOCOFFANC.LOCATION = LOCOFF.LOCATION
INNER JOIN MAXPRD.LOCATIONS LOCOFFDESC
ON LOCOFFANC.ANCESTOR = LOCOFFDESC.LOCATION
INNER JOIN MAXPRD.LOCHIERARCHY LOCOFFHIER
ON LOCOFFANC.ANCESTOR = LOCOFFHIER.LOCATION
WHERE LOCOFF.SITEID = 'SUB'
AND LOCOFFHIER.PARENT = '2000'
GROUP BY WO.WONUM,
LOCOFFDESC.DESCRIPTION,
WO.STATUS,
WO.LOCATION,
LOCOFF.EXT_LOC_HIERARCHY_PATH,
LOCOFF.SITEID,
LOCOFFHIER.PARENT,
WO.PARENT,
WO.WORKTYPE,
WO.REPORTEDBY,
WO.REPORTDATE,
WO.ACTFINISH,
WO.SITEID
答案 0 :(得分:1)
您将计算放在SELECT语句之后,在FROM语句之前,因此您有一个用计算值填充的列,类似于excel。
我们需要知道您正在使用哪种数据库系统来获取确切的语法。你可以试试WO.SITEID,
SELECT WO.WONUM,
LOCOFFDESC.DESCRIPTION AS OFFICE,
WO.STATUS,
WO.LOCATION,
CASE
WHEN ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) = 0)
THEN SUBSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2)), LENGTH(LOCOFF.EXT_LOC_HIERARCHY_PATH))
WHEN ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) IS NOT NULL)
THEN SUBSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, ((INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2)), (INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 2, 3)) - (INSTR(LOCOFF.EXT_LOC_HIERARCHY_PATH, '/', 1, 2) + 2))
END AS "SUBSTATION CASE",
WO.PARENT,
WO.WORKTYPE,
WO.REPORTEDBY,
WO.REPORTDATE,
WO.ACTFINISH,
WO.SITEID,
(
CASE
WHEN (
CASE WHEN WO.STATUS != 'Comp'
THEN SYSDATE-WO.REPORTDATE
ELSE WO.ACTFINISH-WO.REPORTDATE
END) < 0
THEN 0
ELSE
CASE WHEN STATUS != 'COMP'
THEN SYSDATE-WO.REPORTDATE
ELSE WO.ACTFINISH-WO.REPORTDATE
END
END) as age
FROM LOCATIONS LOCOFF
RIGHT JOIN MAXPRD.WORKORDER WO
ON WO.LOCATION = LOCOFF.LOCATION
INNER JOIN MAXPRD.LOCANCESTOR LOCOFFANC
ON LOCOFFANC.LOCATION = LOCOFF.LOCATION
INNER JOIN MAXPRD.LOCATIONS LOCOFFDESC
ON LOCOFFANC.ANCESTOR = LOCOFFDESC.LOCATION
INNER JOIN MAXPRD.LOCHIERARCHY LOCOFFHIER
ON LOCOFFANC.ANCESTOR = LOCOFFHIER.LOCATION
WHERE LOCOFF.SITEID = 'SUB'
AND LOCOFFHIER.PARENT = '2000'
GROUP BY WO.WONUM,
LOCOFFDESC.DESCRIPTION,
WO.STATUS,
WO.LOCATION,
LOCOFF.EXT_LOC_HIERARCHY_PATH,
LOCOFF.SITEID,
LOCOFFHIER.PARENT,
WO.PARENT,
WO.WORKTYPE,
WO.REPORTEDBY,
WO.REPORTDATE,
WO.ACTFINISH,
WO.SITEID
为MS SQL服务器编辑。编辑AGAIN for Oracle:)