mysql - Count duplicate rows in SQL -


suppose have following data structure:

s_cui   o_cui   year d000001 d000001 1975 d000001 d000001 1976 d000001 d002413 1976 d000001 d002413 1979 d000001 d002413 1987 d000001 d004298 1976 d000002 d000002 1985 d000003 d000900 1975 d000003 d000900 1990 d000003 d004134 1983 d000003 d004134 1986 

i need count duplicate pairs (in 1st , 2nd column) , each such pair assign lowest value 3rd column. example output should be:

s_cui   o_cui   freq  year d000001 d000001 2     1975 d000001 d002413 3     1976 d000001 d004298 1     1976 d000002 d000002 1     1985 d000003 d000900 2     1975 d000003 d004134 2     1983 

my initial attempt here (without year field):

select s_cui, o_cui, count(*) table group s_cui, o_cui; 

you're close! need add min(year).

it looks want earliest occurrence of year each s_cui, o_cui , count.

select s_cui, o_cui, count(*) freq, min(`year`) `year` table  group s_cui, o_cui; 

min(year) return earliest year each pairing of s_cui, , o_cui while still retaining counts. put ` around year reserved word in mysql.

though don't see how "pairing" takes effect more of each "group"


Comments