我正在建立报告,显示不同部门的一个工单的部分。 请考虑下表
work_order part dept_code datein
aaaa 11 XXX 20/01/2017
aaaa 12 BBB 22/01/2017
bbbb 12 XXX 20/01/2017
bbbb 15 XXX 22/01/2017
cccc 13 XXX 20/01/2017
cccc 17 XYX 20/01/2017
现在我想以这样的方式对这些数据进行分组,即只显示具有不同dept_code的工单。 RESULT
work_order part dept_code datein
aaaa 11 XXX 20/01/2017
aaaa 12 BBB 22/01/2017
cccc 13 XXX 20/01/2017
cccc 17 XYX 20/01/2017
答案 0 :(得分:3)
使用子查询来标识部门计数大于1的work_order
项:
select * from work
where work_order in
(select work_order
from work w2
group by w2.work_order
having count(w2.dept_code) > 1
)
答案 1 :(得分:2)
像这样的东西
SELECT work_order, part, dept_code, datein
FROM [Table]
WHERE work_order in
(
SELECT work_order
FROM [Table]
GROUP BY work_order
HAVING COUNT(DISTINCT dept_code) > 1
)
答案 2 :(得分:2)
你可以使用count()over(),如下所示:
Select * from (
Select *, cnt = Count(dept_code) over(partition by work_order, dept_code) from yourTable ) a
Where a.cnt = 1