同一查询中的计数和列名称

时间:2014-01-25 02:19:30

标签: sql

我的查询

SELECT COUNT(DISTINCT alloc.[code4]) AS countcode4,code4, table2.itemid  as tbitemid2   
   FROM table1,table 2
WHERE table1.id=table2.id
GROUP BY code4, tbitemid2

我希望查询给我计数并仅返回'code4'的一个实例

我该怎么做?

由于 Mithil

1 个答案:

答案 0 :(得分:0)

删除code4] from the组by,并使用聚合函数:

SELECT COUNT(DISTINCT alloc.[code4]) AS countcode4, min(code4) as code4,
       table2.itemid  as tbitemid2   
FROM table1 join
     table2
     on table1.id = table2.id
GROUP BY tbitemid2;

我不知道alloc是什么;它来自您的示例查询。