如何使用GROUP BY为不同的列获取多个SUM()

时间:2015-11-02 14:15:33

标签: sql sql-server tsql group-by sum

这就是我所拥有的:

SELECT     
    SUBJECT_ID, SUM(SOMETABLE.COLUMN) AS HOURS, POINTS, SEMESTER_ID
FROM       
    SOME_TABLES
WHERE     
    (GROUP = (SELECT TOP (1) GROUP
              FROM SOMETABLE2
              WHERE (STUDENT_ID = 123)))
GROUP BY 
    SUBJECT_ID, POINTS, SEMESTER_ID
HAVING      
    (SUBJECT_ID = 782)

此查询返回:

enter image description here

我需要得到这个结果:

enter image description here

为了得到这些结果,我使用了这个查询:

SELECT     
    SUBJECT_ID, SUM(SOMETABLE.COLUMN) AS HOURS,  
    SUM(SOMETABLE3.COLUMN) AS POINTS, SEMESTER_ID
FROM
    SOME_TABLES
WHERE     
    (GROUP = (SELECT TOP (1) GROUP
              FROM SOMETABLE2
              WHERE (STUDENT_ID = 123)))
GROUP BY 
    SUBJECT_ID, SEMESTER_ID
HAVING      
    (SUBJECT_ID = 12)

但是它返回SUM而不包括GROUP BY语句 - 就像第二次截图一样,但是有两次有16个点,而每个学期应该有两行8个点。

如何获得SEMESTER_ID的正确点数?有脚本和样本数据  在这篇文章的评论中。

5 个答案:

答案 0 :(得分:1)

请改为尝试:

SELECT
     SUBJECT_ID,SEMESTER)ID
    ,SUM(HOURS) as HOURS
    ,SUM(POINTS) as POINTS
FROM SOME_TABLES
WHERE SUBJECT_ID = 12
GROUP BY
     SUBJECT_ID,SEMESTER)ID

答案 1 :(得分:0)

您可以使用OVER子句,例如:

<强> QUERY

select distinct 
       Id,
       sum(Hours) over (partition by SimId) Hours,
       sum(Points) over (partition by SimId) Points,
       SimId  
from #t

示例数据

create table #t
(
    Id INT,
    Hours INT,
    Points INT, 
    SimId INT
)

insert into #t values
(787,100,0,214858),
(787,0,8,214858),
(787,100,8,233562),
(787,0,0,233562)

<强>输出

Id  Hours   Points  SimId
787 100     8       214858
787 100     8       233562

<强>更新

这是因为您没有提供示例数据,您可以在以下内容中使用CTE

WITH cte AS
(
SELECT     
    SUBJECT_ID, SUM(SOMETABLE.COLUMN) AS HOURS,  
    SUM(SOMETABLE3.COLUMN) AS POINTS, SEMESTER_ID
FROM
    SOME_TABLES
WHERE     
    (GROUP = (SELECT TOP (1) GROUP
              FROM SOMETABLE2
              WHERE (STUDENT_ID = 123)))
GROUP BY 
    SUBJECT_ID, SEMESTER_ID
HAVING      
    (SUBJECT_ID = 12)
)
SELECT DISTINCT SUBJECT_ID,
       sum(Hours) over (partition by SEMESTER_ID) Hours,
       sum(Points) over (partition by SEMESTER_ID) Points,
       SEMESTER_ID  
FROM cte

更新2

您可以创建#temp_table并在选择查询中插入数据,之后使用CTE

;WITH cte AS
(
SELECT * 
FROM #temp_table
)
SELECT DISTINCT SUBJECT_ID,
       sum(Hours) over (partition by SEMESTER_ID) Hours,
       sum(Points) over (partition by SEMESTER_ID) Points,
       SEMESTER_ID  
FROM cte

答案 2 :(得分:0)

考虑使用SELECT SUBJECT_ID, studentHours.hoursWorked AS HOURS, studentPoints.pointsEarned AS POINTS, SEMESTER_ID FROM SOME_TABLES CROSS APPLY ( SELECT SUM(someColumn) as hoursWorked FROM SOMETABLE2 WHERE STUDENT_ID = 123 ) studentHours CROSS APPLY ( SELECT SUM(someColumn) as pointsEarned FROM SOME_POINTS_TABLE WHERE STUDENT_ID = 123 ) studentPoints WHERE SUBJECT_ID = 12 GROUP BY SUBJECT_ID, SEMESTER_ID 来获取每列的总和。

{{1}}

答案 3 :(得分:0)

使用sample table查询应如下所示:

WITH cte AS (SELECT SUBJECT_ID, HOURS, POINTS, SEMESTER_ID FROM SAMPLE_TABLE2 GROUP BY HOURS, POINTS, SUBJECT_ID, SEMESTER_ID ) SELECT DISTINCT SUBJECT_ID, sum(Hours) over (partition by SEMESTER_ID, SUBJECT_ID) Hours, sum(Points) over (partition by SEMESTER_ID, SUBJECT_ID) Points, SEMESTER_ID FROM cte

关键是:
sum(Hours) over (partition by SEMESTER_ID, SUBJECT_ID ) hours
和:
GROUP BY HOURS, POINTS, SUBJECT_ID, SEMESTER_ID内的INNER QUERY

感谢@StanislovasKalašnikovas!

答案 4 :(得分:-1)

不确定它是否有用,但TOP子句通常不需要括号。 编辑:为了最佳实践,在使用TOP子句时建议使用ORDER BY子句。您也可以尝试ORDER BY和LIMIT。这可能有所帮助。