C# 如何根据技能和出勤情况查询总人数?
如何根据出勤率对可用用户进行合计,并参考他们的技能 在我的Sqlite数据库中,我有两个表:C# 如何根据技能和出勤情况查询总人数?,c#,sqlite,C#,Sqlite,如何根据出勤率对可用用户进行合计,并参考他们的技能 在我的Sqlite数据库中,我有两个表: TABLE: Skill Available +----------+--------+---------+---------+---------+ | Username | Skill_1| Skill_2 | Skill_3 | Skill_4 | +----------+--------+---------+---------+---------+ | Mark | 1 | 1
TABLE: Skill Available
+----------+--------+---------+---------+---------+
| Username | Skill_1| Skill_2 | Skill_3 | Skill_4 |
+----------+--------+---------+---------+---------+
| Mark | 1 | 1 | 1 | 1 |
+----------+--------+---------+---------+---------+
| Jordan | 1 | 0 | 1 | 0 |
+----------+--------+---------+---------+---------+
| John | 1 | 1 | 0 | 0 |
+----------+--------+---------+---------+---------+
| Edward | 1 | 1 | 0 | 0 |
+----------+--------+---------+---------+---------+
Note: Zero represents users that doesn't have that skill. (1/0 = true/false)
TABLE: Attendance
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Username | Site | Shift | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Mark | Bldg1 | Night | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Jordan | Bldg1 | Night | 1 | 1 | 0 | 0 | 1 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| John | Bldg2 | Day | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Edward | Bldg1 | Night | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
Note: Zero represents restday. (1/0 = true/false)
通过使用上面的两个表,我如何在查询中获得这个结果
+-----------+-----+-----+-----+-----+-----+-----+-----+
| SkillList | SUN | MON | TUE | WED | THU | FRI | SAT |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_1 | 4 | 3 | 2 | 2 | 3 | 3 | 3 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_2 | 3 | 3 | 1 | 1 | 2 | 2 | 2 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_3 | 2 | 2 | 1 | 0 | 2 | 1 | 1 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_4 | 1 | 1 | 0 | 0 | 1 | 0 | 0 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
我测试过的这个将产生你想要的结果。这可能不是最优雅、最有表现力的方式,但我只是匆匆忙忙地做了这件事。 不管怎样,希望会有所帮助:
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。在sql server中,您可以使用PIVOT和window函数来实现这一点,但您也可以使用sqlite内置功能来执行类似的操作,以避免脚本中的重复。考勤表中的数字是多少?它们是真/假吗?如果是这样的话,两个表之间就没有关系了。是的,有布尔值。得出结果的逻辑是什么?