c# - 如何根据技能和出勤率查询基地总人数?
问题描述
如何根据他们的技能参考出勤率来汇总可用用户群?
在我的 Sqlite 数据库中,我有 2 个表:
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
附言。这是在 sql server db 上完成的,因此您可能会看到一些语法差异。极不可能,但如果您确实使用了 sqlite 对应项。
ps2。在 sql server 中,您可以使用 PIVOT 和窗口函数来实现此目的,但您可以使用 sqlite 内置功能来执行类似的操作,以避免脚本中的重复。
推荐阅读
- apache - Docker 权限被拒绝 - apache 映像 - Hello World
- sql - Nuxeo DAM 系统:通过文件获取文档:内容/数据
- javascript - 添加类并在页面重新加载时和之后保留它
- bash - 如何从文件中剪切并将单词放在同一行Linux
- node.js - 与 NodeJS 中的 Startup.cs 等效的 Azure Functions
- typescript - typescript: Infer generic requirement of type
- c++ - Having an error code C2676 with an iterator to a vector of customtype
- c++ - 如何使这个带有可变参数的模板特化成为类的朋友?
- apk - 将 APK 文件上传到 Google Play 控制台时出错,因为它一直说我有错误的证书
- javascript - JS,CSS中的文本/卡片旋转