简体   繁体   中英

SQL Query to calculate Total in hh:mm:ss Format

I have a query wherein I am calculating time difference between following three actions

Production stop & Production start in hh:mm:ss format
Quality stop and quality start in hh:mm:ss format
shipping stop and shipping start in hh:mm:ss format

I want to some all three time differences which are in hh:mm:ss format. How cna I do that in SQL query?

MY query looks like following:

SELECT 

L.LOT_ID , 
L.LOT_ID_RECORD,
L.MAT_ID
L.QUANTITY

CONVERT(VARCHAR,LR.DT_PRODUCT_START,120) AS PRODUCTION_START,

ISNULL(CONVERT(VARCHAR,LR.DT_PRODUCT_ED,120) , 'NA' ) AS PRODUCTION_STOP,

convert(varchar(5),DateDiff(s, LR.DT_PRODUCT_START, LR.DT_PRODUCT_END)/3600)+':'+convert(varchar(5),DateDiff(s, LR.DT_PRODUCT_START, LR.DT_PRODUCT_END)%3600/60)+':'+convert(varchar(5),(DateDiff(s, LR.DT_PRODUCT_START, LR.DT_PRODUCT_END)%60)) as PRODUCTION_TIME,

CONVERT(VARCHAR,QC.QC_START,120) AS QC_START,

CONVERT(VARCHAR,QC.QC_CLOSE,120) AS QC_CLOSE,

convert(varchar(5),DateDiff(s, QC.QC_START ,QC.QC_CLOSE)/3600)+':'+convert(varchar(5),DateDiff(s, QC.QC_START , QC.QC_CLOSE)%3600/60)+':'+convert(varchar(5),(DateDiff(s, QC.QC_START,QC.QC_CLOSE)%60)) as QUALITY_TIME,

convert(varchar(5),DateDiff(s, S.SHIPPING_START, S.SHIPPING_CLOSE)/3600)+':'+convert(varchar(5),DateDiff(s, S.SHIPPING_START, S.SHIPPING_CLOSE)%3600/60)+':'+convert(varchar(5),(DateDiff(s, S.SHIPPING_START, S.SHIPPING_CLOSE)%60)) as SHIPPING_TIME

FROM XYZ 

I want to sum PRODUCTION_TIME , QUALITY_TIME and SHIPPING_TIME and the result should be in hh:mm:ss format.

I'm not sure how to achieve that? Can you please help?

Regards !

Your posted query seems to be incomplete. IMHO, the best work around would be convert the three fields (to be sum up) to seconds then add those. Finally reconvert seconds to HH:MM:SS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM