sql - 在SQL开发人员中创建IF语句以计算AGE

sql - 在SQL开发人员中创建IF语句以计算AGE,第1张

我在使用Maximo运行的Excel中有此报告。我在AGE列N中有forumla计算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,ACTUALFINISH-REPORTED DATE))<0,0,IF(STATUS<>'COMP", TODAY() - REPORTED DATE, ACTUAL FINISH - REPORTED DATE))

这是我当前的SQL查询:

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

最佳答案:

1 个答案:

答案 0 :(得分:0)

以下是在T-SQL中计算年龄的方法

select case 
            when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
                then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
            else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
        end as MemberAge
go
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复