this will be the first question I posted here, so I apologize for any inadvertent omissions in etiquette.
In my current project, I took a large, non-standardized table and split it into four separate normalized tables. My ultimate goal with which I refer to this board is to create a view that mimics an unnormalized table for backward compatibility.
To provide a simplified snapshot of my script, the essence of what I'm trying to do is two tables:
ASSOC_ROLE ASSOCIATE ---------- ---------- assoc_id (fk) assoc_id (pk) role_id (fk) last_name org_nbr (fk)
So, if I issue the following request ...
SELECT Assoc_Role.org_nbr, Assoc_Role.assoc_id, Associate.last_name, Assoc_Role.role_id FROM Assoc_Role INNER JOIN Associate ON Assoc_Role.assoc_id = Associate.assoc_id WHERE Assoc_Role.org_nbr = '1AA'
... I get the following result set
org_nbr assoc_id last_name role_id ------- -------- --------- ------- 1AA 1447 Cooper 1 1AA 1448 Collins 3 1AA 1448 Collins 4 1AA 1448 Collins 5 1AA 1449 Lynch 6
Ultimately, the view I'd like to build would look something like this:
org_nbr role1_ID role1_name role2_ID role2_name role3_ID role3_name role4_ID role4_name role5_ID role5_name role6_ID role6_name ------- -------- ---------- -------- ---------- -------- ---------- -------- ---------- -------- ---------- -------- ---------- 1AA 1447 Cooper NULL NULL 1448 Collins 1448 Collins 1448 Collins 1449 Lynch
My initial thought was to try using the PIVOT command, but I understand that PIVOT requires some sort of aggregation, and this does not fit my scenario. I also played with the CASE team in the SELECT clause, but that does not smooth my result down to a single record.
Hope someone can shed some light on how I can do this. Let me know if anyone needs more information. Thanks!
Cattle
sql sql-server-2005 pivot
ScottieByNature
source share