![](/img/trans.png)
[英]Why am I Getting an Ambiguous Column name CountryID error message when trying to execute this SQL statement:
[英]Getting an Ambiguous column name error when trying to subquery
提示為假設風險為“1”的設施每年至少需要進行 3 次檢查。 顯示 2013 年未能滿足此要求且至少有 1 次未通過 Canvass 類型檢查的設施。顯示設施名稱以及它在 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
不明確的列名稱“風險”。
我只會使用聚合。 假設 table foodinspectionoriginal
表結構foodinspectionoriginal
:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.