DB2中的SQL服务器查询

时间:2014-05-21 14:38:07

标签: db2 db2-400

全部,  有一个问题。我有一个在sql server db中运行良好的查询。要在DB2中运行相同的操作,需要完成。

SELECT 

     EMPID
    ,TOTALSECONDS /3600 AS Hours
    ,((TOTALSECONDS % 3600) /60) AS Minutes
    ,(TOTALSECONDS % 60) AS Seconds
    ,STATUS
    ,[DATE]
    FROM
    (SELECT
    SUM(DATEDIFF(ss,STARTDATETIME,ENDDATETIME)) AS TOTALSECONDS,
    EMPID,STATUS,
    CONVERT(VARCHAR,STARTDATETIME,10) AS [DATE]
    FROM <TABLE>
    WHERE CONVERT(DATE,STARTDATETIME) = 'xxxx-xx-xx'
    GROUP BY EMPID,STATUS,CONVERT(VARCHAR,STARTDATETIME,10)) AS SUMMARY
    ORDER BY STATUS,DATE

感谢。

1 个答案:

答案 0 :(得分:0)

您的问题是DB2没有DATEDIFFCONVERT函数。有类似的名为TIMESTAMPDIFFVARCHAR_FORMAT

我认为这个查询将是您想要的,但我不能100%确定我是否已正确地将SQL Server格式转换为DB2格式。 :)

SELECT
     EMPID
    ,TOTALSECONDS /3600 AS Hours
    ,(MOD(TOTALSECONDS, 3600) /60) AS Minutes
    ,(MOD(TOTALSECONDS, 60) AS Seconds
    ,STATUS
    ,DATE
FROM (
    SELECT
         SUM(TIMESTAMPDIFF(2, CHAR(ENDDATETIME-STARTDATETIME))) AS TOTALSECONDS
        ,EMPID
        ,STATUS
        ,VARCHAR_FORMAT(STARTDATETIME,'mm-dd-yy') AS DATE
    FROM <TABLE>
    WHERE VARCHAR_FORMAT(STARTDATETIME,'yyyy-mm-dd') = 'xxxx-xx-xx'
    GROUP BY
         EMPID
        ,STATUS
        ,VARCHAR_FORMAT(STARTDATETIME,'mm-dd-yy')
) A
ORDER BY
     STATUS
    ,DATE