2012-10-30 6 views
0

SQL 문에서 중복 된 내용을 제거하려고합니다. 고객 ID, 이름 및 성 customerID로 그룹화 할 수 있고 중복 된 고객 ID 양식, 이름 및 성을 제거 할 수 있습니다. 최종 출력그룹 고객 정보 연락처

SELECT customers.customerid, 
     customers.title, 
     customers.firstname, 
     customers.lastname, 
     customers.postion, 
     company.companyname, 
     (SELECT label.labelcontacttype 
     FROM label 
     WHERE label.labelcontacttypeid = 
     customer_contacts.labelcontacttypeid)AS 
     contactType, 
     customer_contacts.contactdetails, 
     customer_contacts.status, 
     customer_contacts.notes 
FROM customers 
     INNER JOIN customer_company 
       ON customers.customerid = customer_company.customerid 
     INNER JOIN company 
       ON customer_company.companyid = company.companyid 
     INNER JOIN customer_contacts 
       ON customers.customerid = customer_contacts.customerid 

전류 출력

15 Mr Mike Smith Web Developer compudata Email [email protected] 1 dvv 
15 Mr Mike Smith Web Developer compudata Phone 111-111-1111 1 ex:2222 
+0

무엇이 질문입니까? 입력은 무엇입니까? 무슨 일 이니? – LittleBobbyTables

+2

dups의 이유는 'contacttype'이 다른 값과 다르다는 것입니다. 어느 값을 표시하고 싶습니까? 또는 데이터를 어떻게 표시 하시겠습니까? – Taryn

답변

0

정말 당신이하려고하는,하지만 당신은 단지 CustomerID를 원하는 경우, 제목, 이름 및 성 한 번 나타나는 것을 매우 분명, 당신은 사용할 수 있습니다되지 않습니다

select 
    case when rn = 1 then customerid else '' end customerid, 
    case when rn = 1 then title else '' end title, 
    case when rn = 1 then firstname else '' end firstname, 
    case when rn = 1 then lastname else '' end lastname, 
    postion, 
    companyname, 
    contactType, 
    contactdetails, 
    status, 
    notes 
from 
(
    SELECT cast(customers.customerid as varchar(20)) customerid, 
     customers.title, 
     customers.firstname, 
     customers.lastname, 
     customers.postion, 
     company.companyname, 
     (SELECT label.labelcontacttype 
      FROM label 
      WHERE label.labelcontacttypeid = 
     customer_contacts.labelcontacttypeid)AS contactType, 
     customer_contacts.contactdetails, 
     customer_contacts.status, 
     customer_contacts.notes , 
     row_number() over(order by customers.customerid) rn 
    FROM customers 
     INNER JOIN customer_company 
       ON customers.customerid = customer_company.customerid 
     INNER JOIN company 
       ON customer_company.companyid = company.companyid 
     INNER JOIN customer_contacts 
       ON customers.customerid = customer_contacts.customerid 
) src