2

我试图弄清楚如何完成我的查询。任何帮助将不胜感激!

这就是我目前使用的。

SELECT 
   co, queuedTime, startTime, completeTime, 
   DATEDIFF(MINUTE, queuedTime, completeTime) AS elapsedTime 
FROM 
   SJob
WHERE 
   jobClass = 'PayrollFinish' 
   AND DATEDIFF(DAY, queuedTime, GETDATE()) = 0
ORDER BY 
   queuedTime

唯一的问题是我需要queuedTimewherejob class = submitpayrollcompleteTimewherejobclass = payrollfinish

我是 sql 新手,只是在摸索。

4

3 回答 3

1

所以你的查询看起来像这样?

SELECT 
    co, 
    queuedTime, 
    startTime, 
    completeTime, 
    DATEDIFF(MINUTE, queuedTime, completeTime) AS elapsedTime 
FROM 
    SJob
WHERE 
    jobClass = 'PayrollFinish' AND 
    DATEDIFF(DAY,queuedTime,GETDATE())=0 
    and jobClass = 'submitpayroll' AND jobclass = 'payrollfinish'
ORDER BY 
    queuedTime
于 2012-12-20T20:18:20.547 回答
0

如果您只想要其中一个时间,那么这应该可以解决问题。

SELECT  co, 
        CASE time
            WHEN jobClass = 'SubmitPayroll' THEN queuedTime
            WHEN jobClass = 'PayrollFinish' AND DATEDIFF(DAY, queuedTime, GETDATE()) = 0 THEN completeTime
            ELSE 'N/A'
        END,
        startTime,
        DATEDIFF(MINUTE, queuedTime, completeTime) AS elapsedTime 
  FROM SJob
 ORDER BY queuedTime
于 2012-12-20T20:33:59.003 回答
0

试一试,因为我假设您想要来自两个不同记录的数据。

SELECT 
    js.co, 
    js.queuedTime, 
    js.startTime, 
    je.completeTime, 
    DATEDIFF(MINUTE, js.queuedTime, je.completeTime) AS elapsedTime 
FROM 
    SJob js
    JOIN SJob je ON js.co = je.co -- Assuming this is the joining column
    JOIN (SELECT 
            CAST(CONVERT(VARCHAR(50), GETDATE(), 101) AS DATETIME) StartDate, 
            CAST(CONVERT(VARCHAR(50), GETDATE() + 1, 101) AS DATETIME) EndDate) AS date 
        ON ((js.queuedTime >= date.StartDate AND js.queuedTime < date.endDate) OR
            (je.queuedTime >= date.StartDate AND je.queuedTime < date.endDate))
WHERE 
    js.jobClass = 'SubmitPayroll' AND
    je.jobClass = 'PayrollFinish'
ORDER BY 
    js.queuedTime

首先,由于它们似乎是两条单独的记录,因此您需要两次点击表(一次用于SubmitPayroll,另一次用于PayrollFinish记录。由于我不知道您的架构是什么样的,所以我试了一下哪个列连接他们起来。

其次,我不确定您是否希望在特定日期内开始或完成任何一项工作,因为SubmitPayrollPayrollFinish记录不同。

第三,如果您想返回所有提交记录,无论它们是否具有匹配的 Finish 记录,只需将 转换JOIN SJob je为 aLEFT OUTER JOIN即可获得所需的内容(ISNULL尽管.DATEDIFFje.completeTime

同样,这是在没有关于您的架构/数据的更多信息的情况下在黑暗中拍摄的,但希望这能让您更接近您的需求。

于 2012-12-20T20:42:03.080 回答