我想这个查询的答案与here有关,但是我很挣扎。
非常感谢任何帮助。
运行一段代码时出现以下错误消息。
代码在查询表单中工作正常但不在视图中。
代码
SELECT
CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'A%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'B%'
THEN 'Less than 1'
WHEN concat(b.Complete, a.[CALCULATED_AGE]) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
concat(left(b.Complete,2),a.[CALCULATED_AGE],substring(b.Complete,3,100)) AS [Calculated Age],
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2)) AS [Derived_YearMonth of Death],
CONCAT(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),
SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2)) AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
c.[Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
a.[LSOA_OF_RESIDENCE_CODE] AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] as [Havering Quintile],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE concat(a.[NHS_ESTABLISHMENT_IND],'-',a.PLACE_OF_DEATH_ESTAB_TYPE)
END
) AS [NHS ESTABLISHMENT CODE],
isnull(f.[Place of Death Establishment Type],a.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS a
inner join REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
left outer JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT
(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e ON { fn CONCAT(ISNULL(a.ULA_OF_RESIDENCE_CODE, ''), ISNULL(a.WARD_OF_RESIDENCE_CODE, '')) } = e.WD14CDO
LEFT OUTER JOIN REFERENCE.[dbo].[GEOG_NHSCE_EstabTypes_2016] AS f
ON
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE {fn concat({ fn CONCAT(ISNUll(convert(nvarchar,a.[NHS_ESTABLISHMENT_IND],21),''),'-')},isnull(convert(nvarchar,a.PLACE_OF_DEATH_ESTAB_TYPE,21),''))}
END = f.[Code]
LEFT OUTER JOIN REFERENCE.[dbo].[OTH_IMD2015] AS g
ON a.[LSOA_OF_RESIDENCE_CODE] = g.[LSOA code (2011)]
order by [Calculated Age]
上面的代码似乎在concatenate函数前加上{fn,如下所示,如果这有助于任何人解决此问题
SELECT
CASE WHEN aPCMD.Sex = 1 THEN 'M'
WHEN aPCMD.Sex = 2 THEN 'F'
ELSE 'unknown'
END AS Derived_Sex,
CASE WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'A%' THEN 'Less than 1'
WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'B%' THEN 'Less than 1'
WHEN concat(bAgeLookup.Complete, aPCMD.[CALCULATED_AGE]) LIKE 'C%' THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
{ fn CONCAT(LEFT(bAgeLookup.Complete, 2), aPCMD.CALCULATED_AGE, SUBSTRING(bAgeLookup.Complete, 3, 100)) } AS Derived_Age,
YEAR(aPCMD.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(aPCMD.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(aPCMD.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(aPCMD.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
{ fn CONCAT(LEFT(CONVERT(nvarchar, aPCMD.DATE_OF_DEATH, 21), 4), SUBSTRING(CONVERT(nvarchar, aPCMD.DATE_OF_DEATH, 21), 6, 2)) } AS [Derived_YearMonth of Death],
{ fn CONCAT(LEFT(CONVERT(nvarchar, aPCMD.DATE_OF_REGISTRATION, 21), 4), SUBSTRING(CONVERT(nvarchar, aPCMD.DATE_OF_REGISTRATION, 21), 6, 2)) } AS [Derived_YearMonth of Registration],
CASE WHEN LEN(
CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT(CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1] WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code], cICD10.[4 character DESCRIPTION] AS Derived,
cICD10.[3 character Sub-Chapter Description],
cICD10.[Chapter Description],
d.District_Name AS [LA NAME],
e.WD14NM AS [WARD NAME],
aPCMD.LSOA_OF_RESIDENCE_CODE AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] AS [Havering Quintile],
(CASE WHEN aPCMD.[NHS_ESTABLISHMENT_IND] = '1' THEN 'NHS'
WHEN aPCMD.[NHS_ESTABLISHMENT_IND] = '2' THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN 'HOME'
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER' END) AS [PLACE OF DEATH CODE],
(CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN ''
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E' THEN ''
ELSE concat(aPCMD.[NHS_ESTABLISHMENT_IND], '-', aPCMD.PLACE_OF_DEATH_ESTAB_TYPE)
END) AS [NHS ESTABLISHMENT CODE],
ISNULL(f.[Place of Death Establishment Type],
aPCMD.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS aPCMD
INNER JOIN REFERENCE.dbo.[ONS-PCMD_AGE] AS bAgeLookup
ON aPCMD.CALCULATED_AGE_UNIT = bAgeLookup.[Age Unit]
LEFT OUTER JOIN REFERENCE.dbo.[OTH_ICD-10] AS cICD10
ON CASE WHEN LEN(
CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CONCAT( CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END, 'X')
ELSE (CASE WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) = '' THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (aPCMD.UNDERLYING_CAUSE_OF_DEATH) IS NULL THEN aPCMD.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE aPCMD.UNDERLYING_CAUSE_OF_DEATH
END)
END = cICD10.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON aPCMD.ULA_OF_RESIDENCE_CODE = d.ONS_District_Code
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e
ON { fn CONCAT(ISNULL(aPCMD.ULA_OF_RESIDENCE_CODE, ''), ISNULL(aPCMD.WARD_OF_RESIDENCE_CODE, '')) } = e.WD14CDO
LEFT OUTER JOIN REFERENCE.dbo.GEOG_NHSCE_EstabTypes_2016 AS f
ON CASE WHEN aPCMD.PLACE_OF_DEATH_CODE = 'H' THEN ''
WHEN aPCMD.PLACE_OF_DEATH_CODE = 'E' THEN ''
ELSE { fn concat({ fn CONCAT(ISNUll(CONVERT(nvarchar, aPCMD.[NHS_ESTABLISHMENT_IND], 21), ''), '-') },
isnull(CONVERT(nvarchar, aPCMD.PLACE_OF_DEATH_ESTAB_TYPE, 21), '')) }
END = f.Code
LEFT OUTER JOIN REFERENCE.dbo.OTH_IMD2015 AS g ON aPCMD.LSOA_OF_RESIDENCE_CODE = g.[LSOA code (2011)]
答案 0 :(得分:0)
尝试使用以下代码..
SELECT
CASE
WHEN a.Sex = 1
THEN 'M'
WHEN a.Sex = 2
THEN 'F'
END AS Derived_Sex,
CASE
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ ISNULL(a.[CALCULATED_AGE],''))) LIKE 'A%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ ISNULL(a.[CALCULATED_AGE],''))) LIKE 'B%'
THEN 'Less than 1'
WHEN LTRIM(RTRIM(ISNULL(b.Complete,'')+ISNULL(a.[CALCULATED_AGE],''))) LIKE 'C%'
THEN 'Less than 1'
ELSE '1 year and above'
END AS Derived_InfantCat,
left(ISNULL(b.Complete,''),2)+ISNULL(a.[CALCULATED_AGE],'')+substring(ISNULL(b.Complete,''),3,100) AS [Calculated Age],
YEAR(a.DATE_OF_DEATH) AS [Derived_Year of Death],
FORMAT(a.DATE_OF_DEATH, 'MM') AS [Derived_Month of Death],
YEAR(a.DATE_OF_REGISTRATION) AS [Derived_Year of Registration],
FORMAT(a.DATE_OF_REGISTRATION, 'MM') AS [Derived_Month of Registration],
ISNULL(LEFT(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 4),'')+
ISNULL(SUBSTRING(CONVERT(nvarchar, a.DATE_OF_DEATH, 21), 6, 2),'') AS [Derived_YearMonth of Death],
ISNULL(LEFT(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 4),'')+
ISNULL(SUBSTRING(CONVERT(nvarchar, a.DATE_OF_REGISTRATION, 21), 6, 2),'') AS [Derived_YearMonth of Registration],
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
ELSE ISNULL(a.UNDERLYING_CAUSE_OF_DEATH,'')
END+ 'X'
ELSE
(CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END AS [Derived_Cause of Death Code],
c.[4 character DESCRIPTION],
c.[3 character Sub-Chapter Description],
c.[Chapter Description],
d.[District_Name] AS [LA NAME],
e.[WD14NM] AS [WARD NAME],
a.[LSOA_OF_RESIDENCE_CODE] AS LSOA,
g.[Index of Multiple Deprivation (IMD) Score] AS [IMD 2015 Score],
g.[DERIVED_Havering Quintile (1 equals Most Deprived)] as [Havering Quintile],
(
CASE
WHEN a.[NHS_ESTABLISHMENT_IND] = '1'
THEN 'NHS'
WHEN a.[NHS_ESTABLISHMENT_IND] = '2'
THEN 'NON-NHS'
ELSE 'OTHER'
END) AS [NHS ESTABLISHMENT TYPE DESCRIPTION],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN 'HOME'
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN 'ELSEWHERE'
ELSE 'OTHER'
END
) AS [PLACE OF DEATH CODE],
(
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE ISNULL(a.[NHS_ESTABLISHMENT_IND],'')+'-'+ISNULL(a.PLACE_OF_DEATH_ESTAB_TYPE,'')
END
) AS [NHS ESTABLISHMENT CODE],
isnull(f.[Place of Death Establishment Type],a.PLACE_OF_DEATH_CODE) AS [PLACE OF DEATH ESTABLISHMENT TYPE]
FROM RAW.dbo.ONS_Death AS a
inner join REFERENCE.dbo.[ONS-PCMD_AGE] AS b
ON a.CALCULATED_AGE_UNIT = b.[Age Unit]
left outer JOIN REFERENCE.dbo.[OTH_ICD-10] AS c
ON
CASE
WHEN LEN(
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END) = 3
THEN CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN ISNULL(a.[CAUSE_OF_DEATH_ICD_CODE_1],'')
ELSE ISNULL(a.UNDERLYING_CAUSE_OF_DEATH,'')
END+ 'X'
ELSE (
CASE
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) = ''
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
WHEN (a.UNDERLYING_CAUSE_OF_DEATH) IS NULL
THEN a.[CAUSE_OF_DEATH_ICD_CODE_1]
ELSE a.UNDERLYING_CAUSE_OF_DEATH
END)
END = c.[4 CharacterDiagnosis Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_LANames_1991 AS d
ON a.[ULA_OF_RESIDENCE_CODE] = d.[ONS_District_Code]
LEFT OUTER JOIN REFERENCE.dbo.GEOG_WardNames_2014 AS e ON ISNULL(a.ULA_OF_RESIDENCE_CODE, '')+ ISNULL(a.WARD_OF_RESIDENCE_CODE, '') = e.WD14CDO
LEFT OUTER JOIN REFERENCE.[dbo].[GEOG_NHSCE_EstabTypes_2016] AS f
ON
CASE
WHEN a.PLACE_OF_DEATH_CODE = 'H'
THEN ''
WHEN a.PLACE_OF_DEATH_CODE = 'E'
THEN ''
ELSE ISNUll(convert(nvarchar,a.[NHS_ESTABLISHMENT_IND],21),'')+'-'+isnull(convert(nvarchar,a.PLACE_OF_DEATH_ESTAB_TYPE,21),'')
END = f.[Code]
LEFT OUTER JOIN REFERENCE.[dbo].[OTH_IMD2015] AS g
ON a.[LSOA_OF_RESIDENCE_CODE] = g.[LSOA code (2011)]
ORDER BY [Calculated Age]