rspencer
rspencer

Reputation: 43

Populate field based on other fields values

I am trying to create a report and there are five fields in the table defining race. To clean up the report I am trying to create a column in the report that has its data create based off of those. To clarify let me give an example.

Current Fields:

RaceAfroAmer, RaceAmerIndian, Race Caucasion, Race Hispanic, RaceOriental

Each of these fields populates with a 1 or a 0

On the report we need a race column that populates based on the values of those fields, i.e.

Race

If the value of any of those 5 fields equals 1 then the value equals that field name (if RaceAfroAmer = 1 then the race column for that row populates with RaceAfroAmer if not it checks the next field, etc, etc).

Will this require creating a new table that the Race field is auto populated and then we pull the race value from that table? Can it be done directly with SSRS programming?

To clarify this further I am inserting below my full original query for the report, in essence all I am trying to do is take this exact query and add a single race column.

WITH HR_CTE (seq, EmployeeId, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId)
AS
(SELECT ROW_NUMBER() OVER(PARTITION BY E.[EmployeeId] ORDER BY J.EffectiveDate DESC) AS seq,
E.EmployeeID, E.Name, J.JobTitle, J.EffectiveDate, E.SocSecNbr, E.State, E.DateOfBirth, E.DateHired, S.SalaryType, E.CpnyId
FROM XHR_Employee E
JOIN XHR_JobHistory J
    ON E.[EmployeeId] = J.[EmpId]
JOIN XHR_SalaryHist S
    ON E.[EmployeeId] = S.[EmpId])

SELECT EmployeeID, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId
FROM HR_CTE
WHERE seq = 1
AND (CpnyID IN (@CpnyID))
AND (JobTitle IN (@JobTitle))
AND (SalaryType IN (@SalaryType))
ORDER BY Name

When I change this to include the case statement and Race fields I get an error in syntax near the keyword FROM

WITH HR_CTE (seq, EmployeeId, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId, RaceAfroAmer, RaceAmerIndian, RaceCaucasian, RaceHispanic, RaceOriental, RaceOther01, RaceOther02)
AS
(SELECT ROW_NUMBER() OVER(PARTITION BY E.[EmployeeId] ORDER BY J.EffectiveDate DESC) AS seq,
E.EmployeeID, E.Name, JobTitle, J.EffectiveDate, E.SocSecNbr, E.State, E.DateOfBirth, E.DateHired, S.SalaryType, E.CpnyId, E.RaceAfroAmer, E.RaceAmerIndian, E.RaceCaucasian, E.RaceHispanic, E.RaceOriental, E.RaceOther01, E.RaceOther02
FROM XHR_Employee E
JOIN XHR_JobHistory J
    ON E.[EmployeeId] = J.[EmpId]
JOIN XHR_SalaryHist S
    ON E.[EmployeeId] = S.[EmpId])

SELECT EmployeeID, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId, 
CASE
    WHEN RaceAfroAmer = 1 THEN 'Black/African American'
    WHEN RaceAmerIndian = 1 THEN 'American Indian/Alaska Native'
    WHEN RaceCaucasian = 1 THEN 'White'
    WHEN RaceHispanic = 1 THEN 'Hispanic/Latino'
    WHEN RaceOriental = 1 THEN 'Asian'
    WHEN RaceOther01 = 1 THEN 'Native Hawaii/Pacific Islander'
    WHEN RaceOther02 = 1 THEN 'Two Or More Races'
END AS Race,
FROM HR_CTE
WHERE seq = 1
ORDER BY Name

WORKING CODE:

(I took the AS Race out of the bottom of the CASE statement and added the 'Race'= to the end of the select statement.

WITH HR_CTE (seq, EmployeeId, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId, RaceAfroAmer, RaceAmerIndian, RaceCaucasian, RaceHispanic, RaceOriental, RaceOther01, RaceOther02)
AS
(SELECT ROW_NUMBER() OVER(PARTITION BY E.[EmployeeId] ORDER BY J.EffectiveDate DESC) AS seq,
E.EmployeeID, E.Name, JobTitle, J.EffectiveDate, E.SocSecNbr, E.State, E.DateOfBirth, E.DateHired, S.SalaryType, E.CpnyId, E.RaceAfroAmer, E.RaceAmerIndian, E.RaceCaucasian, E.RaceHispanic, E.RaceOriental, E.RaceOther01, E.RaceOther02
FROM XHR_Employee E
JOIN XHR_JobHistory J
    ON E.[EmployeeId] = J.[EmpId]
JOIN XHR_SalaryHist S
    ON E.[EmployeeId] = S.[EmpId])

SELECT EmployeeID, Name, JobTitle, EffectiveDate, SocSecNbr, State, DateOfBirth, DateHired, SalaryType, CpnyId, 'Race'=
CASE
    WHEN RaceAfroAmer = 1 THEN 'Black/African American'
    WHEN RaceAmerIndian = 1 THEN 'American Indian/Alaska Native'
    WHEN RaceCaucasian = 1 THEN 'White'
    WHEN RaceHispanic = 1 THEN 'Hispanic/Latino'
    WHEN RaceOriental = 1 THEN 'Asian'
    WHEN RaceOther01 = 1 THEN 'Native Hawaii/Pacific Islander'
    WHEN RaceOther02 = 1 THEN 'Two Or More Races'
END
FROM HR_CTE
WHERE seq = 1
ORDER BY Name

Upvotes: 1

Views: 7658

Answers (2)

Jamie F
Jamie F

Reputation: 23789

Mark's answer is nice, and keeps the logic in the SQL query. But in case "Can it be done directly with SSRS programming?" means that the OP would like to keep this in the report and not in the query, then there are a couple of ways you can put this into VB formulas for SSRS.

You can create either a calculated field for the dataset or just use a formula in your cell:

=SWITCH(
 Fields!RaceAfroAmer.Value = 1, "RaceAfroAmer",
 Fields!RaceAmerIndian.Value = 1, "RaceAmerIndian",
 Fields!RaceCaucasion.Value = 1, "RaceCaucasion",
 Fields!RaceHispanic.Value = 1, "'RaceHispanic",
 Fields!RaceOriental.Value = 1, "RaceOriental")

Or, you could create multiple placeholders in a cell and change the visibility of those based on the field values.

Upvotes: 1

user359040
user359040

Reputation:

The simplest way to do this would be with a case clause in the query:

select ...
       case
           when RaceAfroAmer = 1 then 'RaceAfroAmer'
           when RaceAmerIndian = 1 then 'RaceAmerIndian'
           when RaceCaucasion = 1 then 'RaceCaucasion'
           when RaceHispanic = 1 then 'RaceHispanic'
           when RaceOriental = 1 then 'RaceOriental'
       end as RaceDescription,
       ...

Upvotes: 2

Related Questions