日期:2014-05-17 浏览次数:20563 次
IF(OBJECT_ID('TA','U') IS NOT NULL) DROP TABLE TA
CREATE TABLE TA(Sname VARCHAR(10),Cname VARCHAR(10),Score INT, xf TINYINT)
INSERT INTO TA
SELECT '张三','语文',40,2 UNION ALL
SELECT '张三','数学',60,5 UNION ALL
SELECT '张三','语文',80,2 UNION ALL
SELECT '张三','英语',54,3 UNION ALL
SELECT '张三','英语',38,3 UNION ALL
SELECT '张三','数学',70,5
SELECT Sname, SUM(xf) AS '总学分' FROM(
SELECT Sname,Cname,xf FROM TA
GROUP BY Sname,Cname,xf
)AS A GROUP BY Sname
---------
SELECT Sname, SUM(xf) AS '获得学分' FROM(
SELECT Sname,Cname,xf FROM TA
WHERE Score>=60
GROUP BY Sname,Cname,xf)AS A
GROUP BY Sname
if OBJECT_ID('Score') is not null
drop table Score
create table Score
(
Name nvarchar(20),
Course nvarchar(20),
Score int,
Credit int
)
go
insert into Score
select '张三','语文',40,2 union all
select '张三','数学',60,5 union all
select '张三','语文',80,2 union all
select '张三','英语',54,3 union all
select '张三','英语',38,3 union all
select '张三','数学',70,5
select *From Score
--统计该学生所学课程的总学分
select S.Name,SUM(S.Credit) CreditAll from
(select Name,Course,Credit from Score
group by Name,Course,Credit) as S
group by Name
--统计该学生及格课程的学分
select Name,SUM(Credit) CreditAll from
(select Name,Course,Credit, MAX(Score) MaxScore from Score
group by Name,Course,Credit
having max(Score)>=60) as S
group by Name