尝试子查询时出现不明确的列名错误
Getting an Ambiguous column name error when trying to subquery
提示为假设风险“1”设施每年至少需要进行 3 次类型检查。显示 2013 年未能满足此要求且至少有 1 个 Failed Canvass-type inspection.Show 设施名称以及 2013 年有多少 Canvass 检查的设施。提示:使用子查询
这是我的代码
SELECT DBAName, COUNT(*)
FROM FoodInspectionOriginal, Organization
WHERE Risk = 1 AND [Inspection Type] IN (SELECT [Inspection Type] FROM FoodInspectionOriginal WHERE [Inspection Type] = 'Canvass' AND [Inspection Date] = Year(2013))
GROUP BY DBAName;
我得到的是输出
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'Risk'.
我只会使用聚合。假设 table foodinspectionoriginal
的以下 table 结构:
dbaname -- name of the facility
inspection_date
inspection_type -- "Canvass", ...
inspection_status -- "successful", "failed"
查询可以这样表述:
select dbaname,
sum(case when inspection_type = 'Canvass' and inspection_status = 'failed' then 1 else 0 end) as cnt_failed_canvass
from foodinspectionoriginal
where
risk = 1
and inspection_date >= '20130101'
and inspection_date < '20140101'
group by dbaname
having
count(*) < 3
and sum(case when inspection_type = 'Canvass' and inspection_status = 'failed' then 1 else 0 end) > 0
提示为假设风险“1”设施每年至少需要进行 3 次类型检查。显示 2013 年未能满足此要求且至少有 1 个 Failed Canvass-type inspection.Show 设施名称以及 2013 年有多少 Canvass 检查的设施。提示:使用子查询
这是我的代码
SELECT DBAName, COUNT(*)
FROM FoodInspectionOriginal, Organization
WHERE Risk = 1 AND [Inspection Type] IN (SELECT [Inspection Type] FROM FoodInspectionOriginal WHERE [Inspection Type] = 'Canvass' AND [Inspection Date] = Year(2013))
GROUP BY DBAName;
我得到的是输出
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'Risk'.
我只会使用聚合。假设 table foodinspectionoriginal
的以下 table 结构:
dbaname -- name of the facility
inspection_date
inspection_type -- "Canvass", ...
inspection_status -- "successful", "failed"
查询可以这样表述:
select dbaname,
sum(case when inspection_type = 'Canvass' and inspection_status = 'failed' then 1 else 0 end) as cnt_failed_canvass
from foodinspectionoriginal
where
risk = 1
and inspection_date >= '20130101'
and inspection_date < '20140101'
group by dbaname
having
count(*) < 3
and sum(case when inspection_type = 'Canvass' and inspection_status = 'failed' then 1 else 0 end) > 0