子查询中的varchar数据类型的聚合子句

时间:2012-05-18 13:46:29

标签: sql

我想用子查询执行以下操作,即使我可以用另一种方式执行。我正在查询一个大型联结表,在该查询内部,我希望计算出超过X次的所有县名。

select v.id, k.countyName
from v inner join k on v.countyID=k.countyID inner join
icd_jxn on v.id = icd_jxn.id
where k.countyName in
(select count(k.countyName) from k
having count(k.countyName) > 10)

给出的错误是conversion failed when converting the varchar value X to int。我不想做任何转换,我只是想要多次将县名出现在桌面上。

enter image description here

4 个答案:

答案 0 :(得分:3)

您的内部查询仅返回计数。您的查询需要是:

select v.id, k.countyName 
from v inner join k on v.countyID=k.countyID inner join 
icd_jxn on v.id = icd_jxn.id 
where k.countyName in 
(select k.countyName from k 
group by k.countyName
having count(k.countyName) > 10) 

答案 1 :(得分:0)

您可以尝试以下

select v.id, k.countyName
from v inner join k on v.countyID=k.countyID 
       inner join icd_jxn on v.id = icd_jxn.id
where k.countyName in
(
    select k.countyName from k
    group by k.countyName
    having count(k.countyName) > 10
)

问题是您选择了count并检查了IN子查询中的varchar类型。因此,您应该选择countyName

答案 2 :(得分:0)

您正在尝试查看国家/地区名称IN的计数(从而将varchar与整数进行比较)

select v.id, k.countyName
from v 
inner join k on v.countyID=k.countyID 
inner join icd_jxn on v.id = icd_jxn.id
where k.countyName in
(
   select k.countyName as c from k
   having count(k.countyName) > 10 
   group by k.countyName
)

答案 3 :(得分:0)

在您的查询中,您正在将k.CountryName varchar类型转换为导致错误的Count int类型。 试试这个

select v.id, k.countyName
from v inner join k on v.countyID=k.countyID inner join
icd_jxn on v.id = icd_jxn.id
where k.countyName in
(select k.countyName from k
group by k.countyName
having count(k.countyName) > 10)