给出我想要的姓名,账户和职位列表:
我当前的查询如下所示:
SELECT *
FROM CTE cte1
JOIN
(
SELECT Name, OppName FROM CTE GROUP BY Name, OppName HAVING COUNT(Name)>1
) as cte2
on cte2.Name = cte1.Name and cte2.OppName = cte1.OppName
ORDER BY cte1.OppName, cte1.Name
我还没有公布CTE查询的其余部分,因为这是很长的方式。 但是,这只是向我提供名称和帐户相同且职位不同的结果。
即。如果Oera在科视作为销售分析师和开发人员工作,那么他只会选择Oera在科视作为开发人员工作的记录。
如何相应地修改此查询?
答案 0 :(得分:0)
你在找这样的东西吗?
SELECT *
FROM CTE AS cte1
JOIN
(
SELECT Name, OppName,COUNT(Name) PARTITION BY (Name,OppName) cnt
FROM CTE
) AS cte2
ON cte2.Name = cte1.Name and cte2.OppName = cte1.OppName
WHERE cnt > 1
ORDER BY cte1.OppName, cte1.Name