Skip to content
Advertisement

Calculate sum of duration SQL

I use the following sql to calculate the duration in the first query.

And get the following which is good enter image description here

And now, I would like to further calculate the total time of different lessons within a week in another query. Something like sum(iif(lesson=”math”,duration,0))

But since it’s time data type sql server doesn’t let me use the sum function.

I tried the following sql.

But it isn’t what I expect because I would like to keep the original format with some criteria.

Is there a neat way to do so? Thank you.

Advertisement

Answer

First, just get the numeric duration:

This you can sum and convert to DateTime:

Example:

Data:

Data

Queries:

Result:

Result

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement