Here’s the situation. I created a view that has all the following information
CREATE VIEW view2 AS
SELECT activity_id,
subject_code,
academic_period_code,
DATEDIFF(HOUR,time_start,time_end) AS duration
FROM activity
Okay, now I’m trying to make a query that can produce two columns,
1. one with subject code and the academic_period code concatenated into one column
2. The second column contains the total number of duration for each subject that is in that academic period
So its like: CS123 in academic_period 121 has total of 5 hours activity. I’m fine with the first column but I’ve been wondering how do I get the 2nd column. If I use the SUM function it just totals up the column itself so I’m not sure how to deal with this.
Sample data
Subject Code Activity_code Academic_Period_Code Duration
CS123 1 121 2
CS123 2 121 3
CS123 3 122 2
CS123 4 122 2
What I have done?]
SELECT subject_code + academic_period_code AS subject,
(I think I have to do a subquery but I'm not sure how its suppose to work out) AS total_activity_time
FROM view2
Assuming SQL Server and that I understand what you are looking for:
should get you where I think you’re trying to get.