我试图找到每天分配给员工的新任务。小提琴是here
员工可以在4个任务A,B,C和C之间循环。 D. 1月1日,他可能会被分配任务,1月2日他可能会被分配任务' B' 1月5日任务' C'第6 Jan任务' D' &安培; 1月14日他说他被分配了任务' A'再次。我希望看到日期&与任何特定日期相比,新分配的任务与前一天相比。
我需要的SQL输出应该只显示他在给定日期分配的新任务。如果给定日期不存在数据,则new的定义与前一天或最后一个条目有关。
更新的SQL
SELECT task_date,
employee_name,
Group_concat(task_name)
FROM (
SELECT DISTINCT a.task_date,
a.employee_name,
CASE
WHEN b.employee_name IS NOT NULL
AND c.employee_name IS NULL THEN NULL
ELSE a.task_name
END AS task_name
FROM forgerock AS a
LEFT OUTER JOIN forgerock AS b
ON a.employee_name = b.employee_name = 'A'
AND a.task_date >= '2015-01-02'
AND a.task_date <= '2015-01-04'
AND b.task_date >= '2015-01-02'
AND b.task_date <= '2015-01-04'
AND a.task_date - 1 = b.task_date
AND a.region = b.region = 'USA'
LEFT OUTER JOIN forgerock AS c
ON a.employee_name = c.employee_name = 'A'
AND a.task_date >= '2015-01-02'
AND a.task_date <= '2015-01-04'
AND c.task_date >= '2015-01-02'
AND c.task_date <= '2015-01-04'
AND a.task_date - 1 = c.task_date
AND a.task_name <> c.task_name
AND a.region = b.region = 'USA'
ORDER BY a.task_date,
a.employee_name,
a.task_name) AS temp
GROUP BY task_date,
employee_name
答案 0 :(得分:1)
select task_date,employee_name,GROUP_CONCAT(task_name) from(
select distinct a.task_date,a.employee_name
,case when b.employee_name is not null and c.employee_name is null
then null
else a.task_name end as task_name
from ForgeRock as a left outer join ForgeRock as b
on a.employee_name = b.employee_name and a.task_date-1 = b.task_date
left outer join ForgeRock as c
on a.employee_name = c.employee_name and a.task_date-1 = c.task_date
and a.task_name <> c.task_name
order by a.task_date,a.employee_name,a.task_name) as temp
group by task_date,employee_name
添加条件:
select task_date,employee_name,GROUP_CONCAT(task_name) from(
select distinct a.task_date,a.employee_name
,case when b.employee_name is not null and c.employee_name is null
then null
else a.task_name end as task_name
from ForgeRock as a left outer join ForgeRock as b
on a.employee_name = b.employee_name and a.task_date-1 = b.task_date and a.task_date between '2015-01-02' AND '2015-01-04' and b.task_date between '2015-01-02' AND '2015-01-04'
left outer join ForgeRock as c
on a.employee_name = c.employee_name and a.task_date-1 = c.task_date and a.task_date between '2015-01-02' AND '2015-01-04' and c.task_date between '2015-01-02' AND '2015-01-04'
and a.task_name <> c.task_name
where a.region = 'USA' and a.task_date between '2015-01-02' AND '2015-01-04'
order by a.task_date,a.employee_name,a.task_name) as temp
group by task_date,employee_name