sql - Count of occurrences per row ignoring the duplicates -


basically, want know sql equivalent of excel's countif(range,criteria)

i want output

call id |  name  | phone number | # of occurrences       0001  | name1  |  0000000000  |        2       0002  | name2  |  1111111111  |        1       0003  | name1  |  0000000000  |        2       0004  | name3  |  2222222222  |        2       0005  | name5  |  4444444444  |        1       0006  | name4  |  3333333333  |        1       0007  | name3  |  2222222222  |        2 

not output

call id |  name  | phone number | # of occurrences       0001  | name1  |  0000000000  |        2       0002  | name2  |  1111111111  |        1       0004  | name3  |  2222222222  |        2       0005  | name5  |  4444444444  |        1       0006  | name4  |  3333333333  |        1     

this simplified table. complex table , result result

what wanted avoid having 2 appointments, instead, list them in separate rows appointment 1

here query

select a."call id" "call id", concat(a.campaign, '-', a."contact id") identifier, replace(a."campaign",'cw-','') "campaign", a."dnis" "dnis", a."contact id" "contact id", a."first_name" "first_name", a."last_name" "last_name", a."city" "city", a."street" "street", a."zip" "zip", a."state" "state", a."number2" "number2", a."number1" "number1", a."customer id - real" "customer id - real", count(a."call id") "dial attempts"  "five9 calls"  group  concat(a.campaign, '-', a."contact id"), replace(a."campaign",'cw-',''), a."contact id", a."first_name", a."last_name", a."city", a."street", a."zip", a."state", a."number2", a."number1" 

declan's answer gives number of names linked each phone number (one row per phone number).

if it's important keep same number of row (i.e. same layout you've described in question), can use over clause.

select name, [phone number], count(name) on (partition [phone number]) occur yourtable 

with over there's no need group by.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -