没有为第2列指定列名称' t'

时间:2017-04-06 03:24:44

标签: sql sql-server-2008

我有这个查询,但它不能正常工作

select t.ACCOUNT_ID 
from 
    (select   
         a.ACCOUNT_ID, count(a.ACCOUNT_ID)  
     from 
         EMPLOYER a, EMPLOYER b
     where 
         a.main_id = b.main_id 
     group by 
         a.MAIN_ID, a.account_id
     having 
        count(a.account_id) > 1) t

我正在尝试查找重复项,并希望从中获取唯一的ACCOUNT_ID 子查询

1 个答案:

答案 0 :(得分:1)

    select t.ACCOUNT_ID from (select   
 a.ACCOUNT_ID,count(a.ACCOUNT_ID) as cnt
from EMPLOYER a,EMPLOYER b
 where a.main_id = b.main_id 
 group by a.MAIN_ID,a.account_id
 having count( a.account_id) > 1)t

如果要从子查询中进行选择,则应至少为每列的第一个实例命名。

as cnt

之后添加了COUNT(a.account_id)