0

我想用子查询来做以下事情,即使我可以用另一种方式来做。我正在查询一个大型联结表,在该查询中,我想计算出现次数超过 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。我不想做任何转换,我只想要县名出现在表格上的次数。

在此处输入图像描述

4

4 回答 4

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) 
于 2012-05-18T13:49:03.343 回答
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
)

问题是您在 IN 子查询中选择count并检查了varchar类型。所以,你应该选择countyName

于 2012-05-18T13:49:51.150 回答
0

您正在尝试查看计数中的国家/地区名称(因此将 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
)
于 2012-05-18T13:50:20.417 回答
0

在您的查询中,您将k.CountryNamevarchar 类型转换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)
于 2012-05-18T13:50:39.850 回答