我有两个表,dimensionStructure和dimensionDetail。当我在Case语句之后加入NOT EXISTS时,一切都很美妙。
SELECT ds.structureName, ds.dimensionName, 'Error' = CASE
when NOT EXISTS (select 1 from dbo.DimensionStructure ds
inner join dbo.DimensionDetail dd on dd.dimensionName = ds.dimensionName
where dd.element!= 'revenue')
THEN 'You must have at least one dimension that is tied to the element Revenue.'
ELSE 'Test'
END
from dbo.DimensionStructure ds
where ds.structureType = 'revenue'
但是,如果我尝试在Case语句之外的dimensionDetail中添加一列,突然间我没有得到任何结果。
SELECT ds.structureName, ds.dimensionName, dd.element, 'Error' = CASE
when NOT EXISTS (select 1 from dbo.DimensionStructure ds
inner join dbo.DimensionDetail dd on dd.dimensionName = ds.dimensionName
where dd.element!= 'revenue')
THEN 'You must have at least one dimension that is tied to the element Revenue.'
ELSE 'Test'
END
from dbo.DimensionStructure ds
inner join dbo.DimensionDetail dd on dd.dimensionName = ds.dimensionName
where ds.structureType = 'revenue'
我还没有完全理解我做错了什么。
答案 0 :(得分:1)
您可以尝试将查询修改为
SELECT ds.structureName,
ds.dimensionName,
dd.element,
CASE
when dd.element != 'revenue'
THEN 'You must have at least one dimension that is tied to the element Revenue.'
ELSE 'Test' END AS 'Error'
from dbo.DimensionStructure ds
inner join dbo.DimensionDetail dd on dd.dimensionName = ds.dimensionName
where ds.structureType = 'revenue';