我有两张桌子
schoolname StudentID Class
A 1 1
A 2 2
B 3 1
B 4 2
C 5 1
C 6 2
C 7 3
和
StudentID Attendance
1 present
2 Absent
3 Absent
4 present
5 present
6 absent
7 absent
我想要像
这样的结果schoolname class1 class2 class3 present absent
A 1 1 0 1 1
B 1 1 0 1 1
c 1 1 1 1 2
class1,2,3,根据studentid
显示和缺席计数示例代码:
; with cte as
(
select schoolname as [School Name]
, count(studentid) as total
, class
from Student_Master
group by SchoolName
, class
)
select [School Name]
,isnull(1,0)1
,isnull(2,0)2
,isnull(3,0)3
from cte
pivot (sum(total) for [class] in (1,2,3) ) as p
答案 0 :(得分:2)
为了方便起见,我使用了表变量,但您可以在最终解决方案中轻松替换永久表名。
示例数据设置:
declare @student_master table
(
schoolname char(1) not null
, studentid int not null
, class int not null
)
insert into @student_master values ('A', 1, 1)
insert into @student_master values ('A', 2, 2)
insert into @student_master values ('B', 3, 1)
insert into @student_master values ('B', 4, 2)
insert into @student_master values ('C', 5, 1)
insert into @student_master values ('C', 6, 2)
insert into @student_master values ('C', 7, 3)
declare @student_attendance table
(
studentid int not null
, attendance varchar(10) not null
)
insert into @student_attendance values (1, 'present')
insert into @student_attendance values (2, 'Absent')
insert into @student_attendance values (3, 'Absent')
insert into @student_attendance values (4, 'present')
insert into @student_attendance values (5, 'present')
insert into @student_attendance values (6, 'absent')
insert into @student_attendance values (7, 'absent')
然后我使用以下查询来获得所需的输出。
<强>答案:强>
; with classes as
(
select post.schoolname
, isnull(post.class1, 0) as class1
, isnull(post.class2, 0) as class2
, isnull(post.class3, 0) as class3
from (
select sm.schoolname
, 'class' + cast(sm.class as varchar(4)) as col_nm
, count(*) as col_val
from @student_master as sm
group by sm.schoolname
, sm.class
) as pre
pivot (sum(pre.col_val) for pre.col_nm in ([class1], [class2], [class3])) as post
)
, attendance as
(
select sm.schoolname
, sum(case when sa.attendance = 'present' then 1 else 0 end) as present
, sum(case when sa.attendance = 'absent' then 1 else 0 end) as [absent]
from @student_attendance as sa
inner join @student_master as sm on sa.studentid = sm.studentid
group by sm.schoolname
)
select c.schoolname
, c.class1
, c.class2
, c.class3
, a.present
, a.[absent]
from classes as c
inner join attendance as a on c.schoolname = a.schoolname
<强>输出:强>
schoolname class1 class2 class3 present absent
A 1 1 0 1 1
B 1 1 0 1 1
C 1 1 1 1 2
答案 1 :(得分:0)
不要从@Tarheel那里得到任何信任,只是采取一些不同的方法。
declare @student_master table
(
schoolname char(1) not null
, studentid int not null
, class int not null
)
insert into @student_master values ('A', 1, 1)
insert into @student_master values ('A', 2, 2)
insert into @student_master values ('B', 3, 1)
insert into @student_master values ('B', 4, 2)
insert into @student_master values ('C', 5, 1)
insert into @student_master values ('C', 6, 2)
insert into @student_master values ('C', 7, 3)
declare @student_attendance table
(
studentid int not null
, attendance varchar(10) not null
)
insert into @student_attendance values (1, 'present')
insert into @student_attendance values (2, 'Absent')
insert into @student_attendance values (3, 'Absent')
insert into @student_attendance values (4, 'present')
insert into @student_attendance values (5, 'present')
insert into @student_attendance values (6, 'absent')
insert into @student_attendance values (7, 'absent')
SELECT schoolname,
SUM(CASE WHEN class = 1 then 1 else 0 end) AS Class1,
SUM(CASE WHEN class = 2 then 1 else 0 end) AS Class2,
SUM(CASE WHEN class = 3 then 1 else 0 end) AS Class3,
SUM(CASE WHEN sa.attendance = 'present' THEN 1 ELSE 0 END) AS Present,
SUM(CASE WHEN sa.attendance = 'absent' THEN 1 ELSE 0 END) AS Absent
FROM @student_attendance AS sa
INNER JOIN @student_master AS sm ON sa.studentid = sm.studentid
GROUP BY schoolname
Output:
schoolname Class1 Class2 Class3 Present Absent
A 1 1 0 1 1
B 1 1 0 1 1
C 1 1 1 1 2