我想創建一個報告,有表中的五個字段定義比賽。爲了清理報告,我試圖在報告中創建一個根據這些數據創建數據的列。澄清讓我舉個例子。填充字段值
當前字段:
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
有沒有可能對這些領域的多個在一個記錄是1?如果是這樣,你是否希望將所有適用的比賽都包含在說明中,或者只是列表中的第一個? – 2012-02-07 16:03:51
@ Mark否,只有其中一個值爲1的行中有一個。 – rspencer 2012-02-07 16:09:50
'END As Race'和'FROM'之間有逗號逗號也許您計劃另一個字段? – JeffO 2012-02-07 16:46:41