sql - DB2 return first match -


in db2 (a.k.a. db2/400) @ v6r1, want write sql select statement returns columns header record , columns 1 of matching detail records. can of matching records, want info 1 of them. able accomplish following query below, i'm thinking there has easier way using clause. i'll use if need it, keep thinking, "there must easier way". essentially, i'm returning firstname , lastname person table ... plus 1 of matching email-addresses personemail table.

thanks!

    theminimumones (     select personid,            min(emailtype) emailtype         personemail      group personid     )     select p.personid,            p.firstname,            p.lastname,            pe.emailaddress       person p       left outer join theminimumones tmo         on tmo.personid = p.personid       left outer join personemail pe         on pe.personid = tmo.personid        , pe.emailtype  = tmo.emailtype      personid   firstname                       lastname                        emailaddress            1   bill                            ward                            p1@home.com             2   tony                            iommi                           p2@cell.com             3   geezer                          butler                          p3@home.com             4   john                            osbourne                        -            

this sounds job row_number():

select p.personid, p.firstname, p.lastname, pe.emailaddress person p left outer join      (select pe.*,              row_number() on (partition personid order personid) seqnum       personemail pe      ) pe      on pe.personid = tmo.personid , seqnum = 1; 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -