我可以显示存在多少重复的分数,但不能显示在哪个部门。
select distinct *
, ROW_NUMBER() over (partition by f.depname order by f.stgrade) as ranked
from (
SELECT COUNT(*) as repeatetive, DepName, stgrade
FROM TBL_DEPARTMANTS
CROSS JOIN TBL_GRADES
group by stgrade, DepName
having count(*) > 1
) as f
答案 0 :(得分:1)
CROSS JOIN
,它会返回多行。partition by f.depname
表达式中的row_number() over (..)
部分。因此,以下查询非常适合您的需求:
select DepName, stgrade,
row_number() over (order by g.stgrade,d.DepName) as ranked
from TBL_DEPARTMANTS d
left join TBL_GRADES g
on g.DeptId = d.Id
group by stgrade, DepName
having count(*) > 1;
假设TBL_GRADES
表中有一列称为DeptId
(否则,您将需要通过JOIN
语句向该查询中再添加一个表)
答案 1 :(得分:0)
您的子查询返回部门以及至少有2行的按等级进行计数。现在,当您在非重复列表中添加COUNT时,您可以从子查询输出中获得部门和等级的唯一值,这将带来一些重复的数据(考虑到COUNT的差异)。如果要获得部门和职系的独特列表,可以尝试以下脚本-
<service
android:name=".AutoCallPickerService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action
android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
答案 2 :(得分:0)
这可能有帮助....
DECLARE @Table TABLE(Id INT IDENTITY(1,1), Department NVARCHAR(10), Score INT)
INSERT INTO @Table VALUES
('A', 10),
('A', 10),
('A', 20),
('A', 40),
('B', 10),
('B', 20),
('B', 20),
('C', 20)
;WITH DupLicateData
AS
(
SELECT Id, Department,Score, COUNT(1) OVER(PARTITION BY Department,Score ORDER BY Department,Score ASC) Rnk
FROM @Table
)
SELECT Id, Department,Score
FROM DupLicateData
WHERE Rnk > 1