sql server 2008 - SQL Count Duplicates where field has same month -
i'm trying count of duplicates of col dp
& col rn
col edate
has particular month.
i have count single occurrences month can't work out how revise count how many duplicates exist particular month.
select count(*) (select * (select dp, rn, edate, count(*)over(partition dp, rn) your_count etable) your_count = 1 , month(edate) = 08) x
example (trying count duplicates of dp & rn month august)
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
result count = 2
based on clarifications original answer, here's revised answer (with original comment below)
select dp, rn, month(edate), count(month(edate)) table1 not exists ( select * table1 t1 t1.dp = table1.dp , t1.rn = table1.rn , month(t1.edate) <> month(table1.edate) ) group dp, rn, month(edate) having count(month(edate)) > 1
how this?
select dp, rn, month(edate), count(month(edate)) etable group dp, rn, month(edate) having count(month(edate)) > 1
http://sqlfiddle.com/#!6/cc9cf/3
the following incorporate year:
select dp, rn, format(edate,'mm-yyyy'), count(format(edate,'mm-yyyy')) etable group dp, rn, format(edate,'mm-yyyy') having count(format(edate,'mm-yyyy')) > 1
Comments
Post a Comment