Here are my query and results:
I want make repeating customer rows into one row with n-columns for however many SecondaryReps there are:
EmpsSalesReps is the table I am selecting from and it will have one row with the employee number of the customer's primary rep and and n-rows with employee numbers who are secondary reps, defined by bPrimary = 1 or 0. Is there a SQL construct that can manipulate this into what I want? Thank you. (Please excuse the terrible formatting. When I edit it, it lines up but when I post it, it does not. I hope it's readable).
Code:
select pri.[customer_id], pri.[EmpID] as PrimaryRep, sec.EmpID as SecondaryRep
from EmpSalesReps pri
left join EmpSalesReps sec on sec.customer_id = pri.customer_id and sec.bPrimary = 0
where (pri.customer_id = '100014' or pri.customer_id = '102709' or pri.customer_id = '102706')
and pri.bPrimary = 1
Code:
customer_id PrimaryRep SecondaryRep
100014 29 18
100014 29 24
100014 29 98
102706 66 42
102709 66 NULL
Code:
customer_id PrimaryRep SecondaryRep SecondaryRep SecondaryRep
100014 29 18 24 98
102706 66 42 NULL NULL
102709 66 NULL NULL NULL