Home > Software engineering >  SQL DateTime Summation
SQL DateTime Summation

Time:04-18

I have like this data:

enter image description here

I want to sum TIME_Column hours in specific year.

Summation of output for 2019 should be 12.5

Summation of output for 2020 should be 14.0

CodePudding user response:

You can try to use DATEPART to get hours & minute numbers from your TIME_Column, the minute numbers might need division by 60, then do SUM aggregate.

SELECT DATEPART(YEAR, DATE_Column),SUM(DATEPART(HOUR, TIME_Column)   DATEPART(MINUTE, TIME_Column)/60.0)
FROM T
GROUP BY DATEPART(YEAR, DATE_Column)

sqlfiddle

NOTE

I would combine TIME_Column & DATE_Column to one column, there might no reason need to spite datetime to two column I think.

  • Related