oracle - SQL Grouping with No Duplicates -


here output. no problem here. want. added distinct id remove duplicates , works in each grouped month.

mn   | cnt ==================== 1    |  1 10   |  2 11   |  5 12   |  5  select extract(month trunc(hdate)) mn, count(distinct id) cnt     schema.travel         (arr = '2' or arr = '3')         ,         hdate between to_date('2015-10-01', 'yyyy-mm-dd') , to_date('2016-09-30', 'yyyy-mm-dd') group extract(month trunc(hdate)); 

but can still possibly have duplicates span more each month. if have record in october , in november same id - want count once - issue

so on course of year or time period - id gets counted once...but still need maintain monthly groupings , output...

??

in other words, want count each id in first month appears.

select extract(month trunc(hdate)) mn, count(distinct id) cnt (select id, min(hdate) hdate       schema.travel t       arr in  '2', '3') ,             hdate between  date '2015-10-01' , date '2016-09-30'       group id      ) t group extract(month trunc(hdate)); 

note: if id appears before '2015-10-01', still count id in first month appears after date. if don't want such id counted @ all, move hdate comparison outer query.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -