SQL server aggregate function query

Viewed 64

I have a table called MusicTrack with the columns MusicTrackID, TrackName and Duration.

I am using the query:

    Select Cast(DateAdd( ms,SUM(DateDiff( ms, '00:00:00', Duration)), '00:00:00' ) as time) 
as 'Total duration' from   
    MusicTrack  where MusicTrackID = '1' or MusicTrackID = '3'

This query adds the durations of the 2 selected music tracks together and displays it in a temporary column called total duration. The 'Duration' is of datatype Time, so I am converting it to integer and back again.

My question: what way can I adapt the query to also include include the TrackName field and a running total duration? Or include the temporary column as well as the TrackName column.

So that the display will have TrackName and Total duration... along the lines of:

TrackName   Duration   Total duration

Name1       00:03:00     00:03:00
Name2       00:03:01     00:06:01

I tried to just include the TrackName column to the query like this, but it doesn't work:

Select TrackName, Cast(....) From MusicTrack  where MusicTrackID = '1' or MusicTrackID = '3'
2 Answers
Related