我正在计算这个。...
CONVERT(varchar(9), CONVERT(DECIMAL(7,3),tblBatchActivity.StopQty /
DATEDIFF( HOUR, tblBatchStatus.Date , tblBatchActivity.LastEdited))) AS Rate
tblBatchActivity.StopQty是数字值
tblBatchStatus.Date,tblBatchActivity.LastEdited是日期和时间值
这是我的完整查询
SELECT
CONVERT(CHAR(10), tblBatch.FinalizedDate, 126) AS FinalizedDate,
tblFinishProduct.FinishProdutDescription,
tblBatchActivity.EndProductCode,
tblBatch.BatchID,
tblPckingType.PackingTitle,
tblBatchActivity.StopQty,
CONVERT(varchar(6), DATEDIFF(second, tblBatchStatus.Date, tblBatchActivity.LastEdited)/3600)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(second, tblBatchStatus.Date, tblBatchActivity.LastEdited) % 3600) / 60), 2)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), DATEDIFF(second, tblBatchStatus.Date, tblBatchActivity.LastEdited) % 60), 2) AS 'TimeTaken (HH:MM:SS)',
Section.SectionName,
CONVERT(varchar(9), CONVERT(DECIMAL(7,3),tblBatchActivity.StopQty / NULLIF(DATEDIFF(HOUR, tblBatchStatus.Date, tblBatchActivity.LastEdited), 0))) AS Rate
FROM tblSection INNER JOIN
tblProductManufacturing ON tblSection.SectionID = tblProductManufacturing.SectionID INNER JOIN
tblSTDBatch INNER JOIN
tblBatch ON tblSTDBatch.STDBatchID = tblBatch.BatchSTDBatchID INNER JOIN
tblBatchActivity ON tblBatch.BatchID = tblBatchActivity.BatchID ON tblProductManufacturing.ProductCode = tblSTDBatch.STDBasicProductCode INNER JOIN
tblFinishProduct ON tblBatchActivity.EndProductCode = tblFinishProduct.FinishProductCode INNER JOIN
tblPckingType ON tblFinishProduct.FinishProdutPackingTypeID = tblPckingType.PackingID INNER JOIN
tblBatchStatus ON tblBatch.BatchID = tblBatchStatus.BatchID
WHERE (CONVERT(date, tblBatch.FinalizedDate) BETWEEN '2019-01-01' AND '2019-01-31') AND
(tblBatch.BatchType = 'Basic') AND
(tblBatchActivity.ActType <> 'Manufacturing') AND
(tblBatchStatus.Status = 8) AND
(tblProductManufacturing.Type = 'Pack')
ORDER BY tblBatch.BatchI
D
答案 0 :(得分:1)
您可以使用NULLIF()忽略0
:
NULLIF(DATEDIFF(HOUR, tblBatchStatus.Date, tblBatchActivity.LastEdited), 0)
使用NULLIF()
的结果将是null
,要忽略null
的值,您可以使用COALESCE()
。
答案 1 :(得分:0)
问题在这里
DATEDIFF( HOUR, tblBatchStatus.Date , tblBatchActivity.LastEdited)
其结果为0,因此tblBatchStatus.Date小时与tblBatchActivity.LastEdited相同
编辑
您可以使用CASE语句 如下
select case when DATEDIFF( HOUR, getdate() , getdate()) <> 0
then CONVERT(varchar(9), CONVERT(DECIMAL(7,3), 50)
/ DATEDIFF( HOUR, getdate() , getdate()))
else CONVERT(varchar(9), CONVERT(DECIMAL(7,3), 50)) -- or different logic
end AS Rate