通过联接其他表将两个额外的列添加到结果集中

时间:2018-07-30 18:00:37

标签: android sqlite join android-room

我有4张桌子:

任务表

 (task_id , department_id , task_title , task_description , task_start_date , task_due_date , task_rating , task_is_completed)

员工表

 (employee_id , department_id , employee_name , employee_salary ,  employee_hire_date)

部门表

 (department_id , department_name)

employees_tasks联接表

 (employee_id , task_id)

每个表都是会议室数据库中的一个实体。

我想用(select * from employees返回2个额外的列 一栏用于计算员工的评分(通过获取任务表中task_rating列的平均值,必须完成任务),另一列用于显示为该员工运行的任务数(通过获取带有{ {1}})

我不知道将哪个表与哪个表联接。我们设法制作了两个单独的SQL语句,通过使用联合和左联接返回了这两列,但是它们很难看,并且在组合它们时不起作用。

我们尝试了什么

task_is_completed = 0

我们希望输出像这样

select employees.employee_name , employees.employee_id ,avg(tasks.task_rating)  as Ratings from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 1 group by (employees.employee_name ) 
union select employees.employee_name, employees.employee_id, avg(0) as Ratings from employees where employees.employee_id  not in (select employees.employee_id from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id ) AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 1 group by (employees.employee_name ) ) group by employees.employee_id order by employees.employee_id ;

select employees.employee_name , employees.employee_id ,count(tasks.task_title)  as tasks_Running from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 0  group by (employees.employee_name ) 
union select employees.employee_name , employees.employee_id ,0   as tasks_Running  from employees  where (employees.employee_id  not in (select employees.employee_id from employees , tasks inner join employees_tasks on(employees.employee_id = employees_tasks.employee_id )AND tasks.task_id = employees_tasks.task_id where tasks.task_is_completed = 0  group by (employees.employee_name )))group by (employees.employee_name) order by employees.employee_id ; 

1 个答案:

答案 0 :(得分:0)

我相信以下内容可能适合:-

WITH 
  -- Common Table Expression 1 - Average of Completed Tasks per employee
    employee_completedtask_info AS (
        SELECT employees.employee_id,avg(tasks.task_rating) AS atr
            FROM employees_tasks
                JOIN tasks ON employees_tasks.task_id = tasks.task_id
                JOIN employees ON employees_tasks.employee_id = employees.employee_id
            WHERE tasks.task_is_completed > 0
            GROUP BY employees.employee_id
    ),
    -- Common Table Expression 2 - Incompleted Taks per employee
    employee_notcompleted_info AS (
        SELECT employees.employee_id,count() AS itc
            FROM employees_tasks
                JOIN tasks ON employees_tasks.task_id = tasks.task_id
                JOIN employees ON employees_tasks.employee_id = employees.employee_id
            WHERE tasks.task_is_completed = 0
            GROUP BY employees.employee_id
    ),
    -- Common Table Expression 3 - Total Tasks per Employee
    employee_total_tasks AS (
        SELECT employees.employee_id,count() AS ttc
            FROM employees_tasks
                JOIN tasks ON employees_tasks.task_id = tasks.task_id
                JOIN employees ON employees_tasks.employee_id = employees.employee_id
            GROUP BY employees.employee_id
    )
    SELECT employees.employee_name, 
        CASE WHEN atr IS NOT NULL THEN atr ELSE 0 END AS average_completed_task_rating,
        CASE WHEN itc IS NOT NULL THEN itc ELSE 0 END AS incomplete_task_count,
        CASE WHEN ttc IS NOT NULL THEN ttc ELSE 0 END AS total_task_count
        FROM employees 
            LEFT JOIN employee_completedtask_info ON employees.employee_id = employee_completedtask_info.employee_id
            LEFT JOIN employee_notcompleted_info ON employees.employee_id = employee_notcompleted_info.employee_id
            LEFT JOIN employee_total_tasks ON employees.employee_id = employee_total_tasks.employee_id
    ;

基于以下生成的数据:-

DROP TABLE IF EXISTS employees;
CREATE TABLE IF NOT EXISTS employees (employee_id INTEGER PRIMARY KEY, department_id INTEGER, employee_name TEXT, employee_salary REAL,  employee_hire_date TEXT);
DROP TABLE IF EXISTS departments;
CREATE TABLE IF NOT EXISTS departments (department_id INTEGER PRIMARY KEY, department_name TEXT);
DROP TABLE IF EXISTS employees_tasks;
CREATE TABLE IF NOT EXISTS employees_tasks (employee_id INTEGER, task_id INTEGER, PRIMARY KEY(employee_id, task_id));

INSERT INTO departments VALUES
    (null,'Maths'),(null,'English'),(null,'Craft')
;
INSERT INTO employees VALUES
    (null,1,'Fred',55000,'2000-01-02'),
    (null,2,'Mary',62000,'1996-03-20'),
    (null,3,'Tom',52000,'2004-10-11'),
    (null,3,'Susan',72000,'1999-06-14'),
    (null,2,'Bert',66000,'2000-10-15'),
    (null,1,'Jane',70000,'1992-04-02')
;
INSERT INTO tasks VALUES
    (null,3,'Task 001 - Craft','Do the Craft thinggy','2018-01-01','2018-08-19',10,0),
    (null,1,'Task 002 - Maths','Do the Maths thinggy','2018-03-14','2019-03-13',20,0),
    (null,2,'Task 003 - English','Do the English thinggy','2018-02-14','2018-09-14',8,0),
    (null,3,'Task 004 - Craft','Do the Craft job','2018-01-01','2018-08-19',10,1),
    (null,1,'Task 005 - Maths','Do the Maths job','2018-03-14','2019-03-13',20,1),
    (null,2,'Task 006 - English','Do the English job','2018-02-14','2018-09-14',8,1),
    (null,3,'Task 007 - Craft','Craft thinggy','2018-03-03','2018-11-21',10,0),
    (null,1,'Task 008 - Maths','Maths thinggy','2018-03-14','2019-03-13',20,0),
    (null,2,'Task 009 - English','English thinggy','2018-02-14','2018-09-14',8,0)
;
INSERT INTO employees_tasks VALUES
    (1,2),(1,5),(1,8),(1,6),
    (2,2),
  (3,1),(3,4),(3,7)
;

结果为:-

enter image description here

  • 注意,这会将空条目转换为0(即,上面没有Susan,Bert和Jane的任务,因此它们的任务计数/平均值为空,这会使情况变得有些复杂,因此,当...则为CASE时... ELSE .... END AS子句)。
  • 请注意,我已列出了总任务计数,因为这可能有用/有害(第三CTE提取了此信息)