我测试的结果会是你想要的。可能不是最优雅和最有表现力的方式,但我只是匆匆忙忙地做了这件事。
select
'Skill_1',
(Select COUNT(*) from Attendance att where att.SUN = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) SUN,
(Select COUNT(*) from Attendance att where att.MON = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) MON,
(Select COUNT(*) from Attendance att where att.TUE = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) TUE,
(Select COUNT(*) from Attendance att where att.WED = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) WED,
(Select COUNT(*) from Attendance att where att.THU = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) THU,
(Select COUNT(*) from Attendance att where att.FRI = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) FRI,
(Select COUNT(*) from Attendance att where att.SAT = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_1 = 1) > 0) SAT
union
select
'Skill_2',
(Select COUNT(*) from Attendance att where att.SUN = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) SUN,
(Select COUNT(*) from Attendance att where att.MON = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) MON,
(Select COUNT(*) from Attendance att where att.TUE = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) TUE,
(Select COUNT(*) from Attendance att where att.WED = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) WED,
(Select COUNT(*) from Attendance att where att.THU = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) THU,
(Select COUNT(*) from Attendance att where att.FRI = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) FRI,
(Select COUNT(*) from Attendance att where att.SAT = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_2 = 1) > 0) SAT
union
select
'Skill_3',
(Select COUNT(*) from Attendance att where att.SUN = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) SUN,
(Select COUNT(*) from Attendance att where att.MON = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) MON,
(Select COUNT(*) from Attendance att where att.TUE = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) TUE,
(Select COUNT(*) from Attendance att where att.WED = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) WED,
(Select COUNT(*) from Attendance att where att.THU = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) THU,
(Select COUNT(*) from Attendance att where att.FRI = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) FRI,
(Select COUNT(*) from Attendance att where att.SAT = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_3 = 1) > 0) SAT
union
select
'Skill_4',
(Select COUNT(*) from Attendance att where att.SUN = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) SUN,
(Select COUNT(*) from Attendance att where att.MON = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) MON,
(Select COUNT(*) from Attendance att where att.TUE = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) TUE,
(Select COUNT(*) from Attendance att where att.WED = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) WED,
(Select COUNT(*) from Attendance att where att.THU = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) THU,
(Select COUNT(*) from Attendance att where att.FRI = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) FRI,
(Select COUNT(*) from Attendance att where att.SAT = 1 and
(select COUNT(*) from SkillsAvailable where Username = att.Username and Skill_4 = 1) > 0) SAT
这是在SQLServerDB上完成的,所以您可能会看到一些语法描述。不太可能,但是如果你只是使用了sqlite对应的。
ps2。在sqlserver中,您可以使用PIVOT和window函数来实现这一点,但是您可以使用sqlite内置的功能来完成类似的工作,以避免脚本中的重复。