我试图获得Col DP
&的重复数量。 Col RN
与Col EDate
有一个特定的月份。
我有这个将计算一个月的所有单次出现但我不知道如何修改这个以计算特定月份存在多少重复。
SELECT count(*) FROM (SELECT * FROM (Select DP, RN, EDate, COUNT(*)OVER(PARTITION BY DP, RN) AS your_count
from ETable) a WHERE your_count = 1 and Month(EDate) = 08) as x
示例(尝试将月份为8月的所有重复的DP& RN计算在内)
DP RN EDate
DP1 RP1 2017-08-09 <--yes
DP2 RP1 2017-08-09
DP3 RP1 2017-08-09 <--yes
DP1 RP2 2017-08-09
DP2 RP1 2017-07-09
DP1 RP1 2017-08-09 <--yes
DP3 RP1 2017-08-09 <--yes
结果 数= 2
答案 0 :(得分:3)
根据原始答案的说明,这里是修改后的答案(下面有原始评论)
select dp, rn, month(edate), count(month(edate))
from table1
where not exists
(
select *
from table1 t1
where t1.dp = table1.dp
and t1.rn = table1.rn
and month(t1.eDate) <> month(table1.eDate)
)
group by dp, rn, month(edate)
having count(month(edate)) > 1
这个怎么样?
select dp, rn, month(edate), count(month(edate))
from eTable
group by dp, rn, month(edate)
having count(month(edate)) > 1
http://sqlfiddle.com/#!6/cc9cf/3
以下内容也将包含年份:
select dp, rn, format(edate,'MM-yyyy'), count(format(edate,'MM-yyyy'))
from eTable
group by dp, rn, format(edate,'MM-yyyy')
having count(format(edate,'MM-yyyy')) > 1
答案 1 :(得分:0)
您可以尝试使用此查询计算存在多少重复项
SELECT SUM(CASE WHEN your_count > 1 THEN 1 ELSE 0 END)
FROM (SELECT DP,
RN,
EDate,
COUNT(*) OVER(PARTITION BY DP, RN) AS your_count
FROM ETable
WHERE Month(EDate) = 08) a