sql server - MS SQL - Display Last Name & First name without changing record -


i have following query uses 2 tables. 1 contact, other email. contact has following columns: contactid, firstname, lastname. email has following columns: emailid, contactid, emailaddress.

i have query runs basic duplicate check. return numeric value , email (i.e., 2 - abc@yahoo.com = 2 contacts have email abc@yahoo.com). i'm trying display first name , last name associated email, when so, changes amount of data being produced. because it's grabbing emails have first , last names attached them well.

here's query; when try input select contact.lastname , contact. firstname, group by, change amount of data outputed; more columns created (expected), there more rows , data less relevant (more nulls returned). what's causing this, , how can fix it?

select count(contact.contactid) [duplicatecount], email.address [emailaddress] contact inner join email on email.contactid = contact.contactid group email.address having count (contact.contactid) > 1 order [duplicatecount] desc 

if ms sql server 2008 or newer can use window function find duplicates. sample query looks this.

select c.firstname, c.lastname, e.emailaddress,        count(c.contactid) over(partition e.emailaddress order e.emailaddress) cnt contact c      inner join email e on c.contactid = e.contactid  order cnt desc 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -