Как запросить общее количество основанных на них навыков и посещаемости? - PullRequest
0 голосов
/ 14 июня 2019

Как подсчитать доступную базу пользователей по посещаемости с указанием их навыков?

В моей базе данных 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   |
+-----------+-----+-----+-----+-----+-----+-----+-----+

1 Ответ

1 голос
/ 14 июня 2019

Это я проверил даст результат, который вы ищете.Возможно, это не самый элегантный и эффективный способ сделать это, но я просто сделал это в спешке.
Надеюсь, все равно будет полезно :)

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, чтобы избежать повторения в сценариях.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...