[英]Getting an Ambiguous column name error when trying to subquery
The prompt is Assume Risk “1” facilities are required to have at least 3-type inspections per year.提示为假设风险为“1”的设施每年至少需要进行 3 次检查。 Display the facilities that failed to meet this requirement in 2013 AND that had at least 1 Failed Canvass-type inspection.Show the facility name and how many Canvass inspections it had in 2013. HINT: use a subquery
显示 2013 年未能满足此要求且至少有 1 次未通过 Canvass 类型检查的设施。显示设施名称以及它在 2013 年进行的 Canvass 检查次数。提示:使用子查询
So this is my code所以这是我的代码
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;
And I am getting this is the output我得到这是输出
Msg 209, Level 16, State 1, Line 3
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'Risk'.
不明确的列名称“风险”。
I would just use aggregation.我只会使用聚合。 Assuming the following table structure for table
foodinspectionoriginal
:假设 table
foodinspectionoriginal
表结构foodinspectionoriginal
:
dbaname -- name of the facility
inspection_date
inspection_type -- "Canvass", ...
inspection_status -- "successful", "failed"
The query could be phrased as follows:查询可以表述如下:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.