Это я проверил даст результат, который вы ищете.Возможно, это не самый элегантный и эффективный способ сделать это, но я просто сделал это в спешке.
Надеюсь, все равно будет полезно :)
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
пс.это делается на БД sql server, поэтому вы можете увидеть некоторые синтаксические описания.Весьма маловероятно, но если вы использовали просто аналог sqlite.
ps2.на сервере sql вы можете добиться этого, используя PIVOT и оконные функции, но вы можете сделать то же самое, используя встроенные функции sqlite, чтобы избежать повторения в сценариях.