我正在尝试创建一个报告,表格中有五个字段定义种族。要清理报告,我试图在报告中创建一个基于其创建数据的列。为了澄清,让我举一个例子。
当前字段:
RaceAfroAmer,RaceAmerIndian,Race Caucasion,Race Hispanic,RaceOriental
每个字段都填充1或0
在报告中,我们需要一个根据这些字段的值填充的竞赛列,即
种族
如果这5个字段中的任何一个的值等于1,那么该值等于该字段名称(如果RaceAfroAmer = 1,则该行的竞争列填充RaceAfroAmer,如果不是,则检查下一个字段等等)。 / p>
这是否需要创建一个新表,Race字段是自动填充的,然后我们从该表中提取竞争值?可以直接用SSRS编程完成吗?
为了进一步澄清这一点,我在报告的完整原始查询下面插入,实质上我想要做的就是采取这个确切的查询并添加一个竞赛列。
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
当我将其更改为包含case语句和Race字段时,我在关键字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
工作代码:
(我将AS Race从CASE语句的底部取出,并将'Race'=添加到select语句的末尾。
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
答案 0 :(得分:2)
执行此操作的最简单方法是使用查询中的case子句:
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,
...
答案 1 :(得分:1)
Mark的答案很好,并且将逻辑保留在SQL查询中。但是“如果可以直接用SSRS编程完成吗?”意味着OP希望将此保留在报表中而不是查询中,那么有几种方法可以将其放入SSRS的VB公式中。
您可以为数据集创建计算字段,也可以只在单元格中使用公式:
=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")
或者,您可以在单元格中创建多个占位符,并根据字段值更改这些占位符的可见性。