Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL DateTime Summation

I have like this columns in sql,

enter image description here

I want to sum TIME_Column hours in specific year.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Summation of output for 2019 should be like this;
12.5

Summation of output for 2020 should be like this;
14.0

Thank you for your help ppl

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading