2012-02-07 201 views
1

我想創建一個報告,有表中的五個字段定義比賽。爲了清理報告,我試圖在報告中創建一個根據這些數據創建數據的列。澄清讓我舉個例子。填充字段值

當前字段:

RaceAfroAmer,RaceAmerIndian,種族種族白種人,種族西班牙,RaceOriental

這些場的每一個1或0

填充在報告中,我們需要一場比賽根據這些字段的值填充的列,即

比賽

如果任何這些5個字段的值等於1,則該值等於該字段名稱(如果RaceAfroAmer = 1,則該行的比賽柱RaceAfroAmer填充如果不是它檢查下一個字段,等等等等)。

這是否需要創建一個新表,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語句和比賽場,我得到的關鍵字附近有語法錯誤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 

工作代碼:

(我把種族在CASE語句的底部,並在選擇語句的末尾添加'Race'=

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

有沒有可能對這些領域的多個在一個記錄是1?如果是這樣,你是否希望將所有適用的比賽都包含在說明中,或者只是列表中的第一個? – 2012-02-07 16:03:51

+0

@ Mark否,只有其中一個值爲1的行中有一個。 – rspencer 2012-02-07 16:09:50

+0

'END As Race'和'FROM'之間有逗號逗號也許您計劃另一個字段? – JeffO 2012-02-07 16:46:41

回答

2

這樣做會與查詢的情況下,條款簡單的方法:

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

馬克的回答是不錯的,並保持邏輯的SQL查詢。但是如果「能否直接使用SSRS編程完成?」指OP想保持這個報告,而不是在查詢中,則有一對夫婦的方式,你可以把這個變成VB公式SSRS。

可以爲數據集創建或者計算的字段或只使用一個公式在單元格:

=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") 

或者,你可以在單元格中創建多個佔位符和改變那些基於該領域的知名度值。