更高级别聚合的总和
Sum of a higher level aggregation
我在下面的代码中定义了一个 table,我希望将所有值的总和作为 ALLDays,在本例中为 Ajan 36 和 Sam 21。我如何修改以下查询以明白了,我不允许使用 windows function.I 可以通过让另一个查询按学生分组并加入两者来实现要求,但是有没有办法修改下面的查询以满足要求。非常感谢任何帮助。
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8)
select student,subject,sum(days) as SubjectDays,'' as AllDays from @Table
group by student,subject
如果您不能使用 window 函数,那么一种替代方法是对学生和学科级别的天数总和使用两个单独的子查询。
select t1.student, t1.subject, t1.SubjectDays, t2.AllDays
from
(
select student, subject, sum(days) as SubjectDays
from @Table
group by student, subject
) t1
inner join
(
select student, sum(days) as AllDays
from @Table
group by student
) t2
on t1.student = t2.student;
也许您可以使用 CTE 来包含总和并加入反对:
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8);
WITH MainSummary (Student,Alldays) AS
(
SELECT Student,SUM([days]) as AllDays
FROM @Table
GROUP BY Student
)
SELECT
T.Student
,T.[subject]
,SUM([Days]) AS SubjectDays
,MAX(MS.AllDays) AS AllDays
FROM @Table AS T
LEFT JOIN MainSummary AS MS ON MS.Student = T.Student
GROUP BY T.Student,T.[Subject]
经过更多搜索,我确定了一种无需任何连接即可满足要求的方法,
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8);
SELECT student,subject,sum(days) as SubjectDays,
(SELECT sum(days) from @Table b where b.Student=a.Student ) as Alldays
FROM @Table a
group by student,subject
我在下面的代码中定义了一个 table,我希望将所有值的总和作为 ALLDays,在本例中为 Ajan 36 和 Sam 21。我如何修改以下查询以明白了,我不允许使用 windows function.I 可以通过让另一个查询按学生分组并加入两者来实现要求,但是有没有办法修改下面的查询以满足要求。非常感谢任何帮助。
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8)
select student,subject,sum(days) as SubjectDays,'' as AllDays from @Table
group by student,subject
如果您不能使用 window 函数,那么一种替代方法是对学生和学科级别的天数总和使用两个单独的子查询。
select t1.student, t1.subject, t1.SubjectDays, t2.AllDays
from
(
select student, subject, sum(days) as SubjectDays
from @Table
group by student, subject
) t1
inner join
(
select student, sum(days) as AllDays
from @Table
group by student
) t2
on t1.student = t2.student;
也许您可以使用 CTE 来包含总和并加入反对:
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8);
WITH MainSummary (Student,Alldays) AS
(
SELECT Student,SUM([days]) as AllDays
FROM @Table
GROUP BY Student
)
SELECT
T.Student
,T.[subject]
,SUM([Days]) AS SubjectDays
,MAX(MS.AllDays) AS AllDays
FROM @Table AS T
LEFT JOIN MainSummary AS MS ON MS.Student = T.Student
GROUP BY T.Student,T.[Subject]
经过更多搜索,我确定了一种无需任何连接即可满足要求的方法,
DECLARE @Table
TABLE(Student varchar(50),
subject varchar(50)
,days int)
Insert into @Table
values('Ajan','English',8),('Ajan','Math',9),('Ajan','Science',7),('Ajan','English',5),('Ajan','Math',4),('Ajan','Science',3),
('Sam','English',7),('Sam','Math',6),('Sam','Science',8);
SELECT student,subject,sum(days) as SubjectDays,
(SELECT sum(days) from @Table b where b.Student=a.Student ) as Alldays
FROM @Table a
group by student,subject