Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T-SQL Trying to pivot data in a SELECT STATMENT, results not being grouped by job

$
0
0

I'm trying to extract data from the vJCJobRoles table which has the following columns:

  1. vJCJobRoles.Job (INT, not null, Key Field)
  2. vJCJobRoles.Role (CHAR, not null, Key Field)
  3. vJCJobRoles.VPUserName (CHAR, not null)

The vJCJobRoles table contains several Roles values for each Job and several VPUserName values for each Role.  What I'm trying to do is create a View from this table that has one record per job and has the values in the Role field for each job as separate columns (field names) and has the VPUserName value in each Job record for each of the Row values.  The result set shown below is giving me multiple records for some jobs in those cases where there is more than one value for the Role field.  Notice I have no aggregating and therefore no Group By Clause.

The SQL code used and result set are shown below.

Thanks for your help.

...bob sutor

SQL CODE:

SELECT vjcjobroles.jcco,
       vjcjobroles.job,
       CASE
         WHEN vjcjobroles.role = 'PM' THEN vjcjobroles.vpusername
       end AS PMUserID,
       CASE
         WHEN vjcjobroles.role = 'SP' THEN vjcjobroles.vpusername
       end AS SPUserID,
       CASE
         WHEN vjcjobroles.role = 'CPC' THEN vjcjobroles.vpusername
       end AS CPCUserID,
       CASE
         WHEN vjcjobroles.role = 'PX' THEN vjcjobroles.vpusername
       end AS PXUserID,
       CASE
         WHEN vjcjobroles.role = 'AA' THEN vjcjobroles.vpusername
       end AS AAUserID,
       CASE
         WHEN vjcjobroles.role = 'APM' THEN vjcjobroles.vpusername
       end AS ADMUserID
FROM   vjcjobroles 

This is the RESULT SET:


Bob Sutor


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>