0

这是情况。我创建了一个包含以下所有信息的视图

CREATE VIEW view2 AS
SELECT activity_id,
       subject_code,
       academic_period_code,
       DATEDIFF(HOUR,time_start,time_end) AS duration
FROM   activity

好的,现在我正在尝试进行一个可以生成两列的查询,1. 一列带有主题代码,并且 Academic_period 代码连接到一列 2. 第二列包含该学术中每个主题的总持续时间时期

就像这样:academic_period 121 中的 CS123 总共有 5 个小时的活动。我对第一列很好,但我一直想知道如何获得第二列。如果我使用 SUM 函数,它只会对列本身进行总计,所以我不确定如何处理这个问题。

样本数据

Subject Code   Activity_code    Academic_Period_Code   Duration
CS123          1                 121                   2
CS123          2                 121                   3
CS123          3                 122                   2
CS123          4                 122                   2

我做了什么?]

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

从视图2

4

2 回答 2

1

假设 SQL Server 并且我了解您在寻找什么:

select
    subject_code + academic_period_code as [Type],
    sum(duration) as duration
from view2
group by
    subject_code + academic_period_code;

应该把你带到我认为你想要到达的地方。

于 2012-05-31T03:38:20.970 回答
0
select
    subject_code + academic_period_code as subjects,
    sum(duration) as duration
from view2
group by
    subject_code, academic_period_code;
于 2012-05-31T05:19:54.753 回答