我正在尝试从我的SQL语句中删除重复的客户ID,firstname和lastName我希望能够按customerID分组并从最终输出中删除重复的客户ID表单,firstname和lastName
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@email.com 1 dvv
15 Mr Mike Smith Web Developer compudata Phone 111-111-1111 1 ex:2222
答案 0 :(得分:0)
你真的不太清楚你想要做什么,但是如果你只想让customerid,title,firstname和lastname出现一次,那么你可以使用:
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