添加字段到查询

时间:2015-08-19 14:25:12

标签: sql

我如何添加"姓名"此查询的字段

    SELECT Address, 
           COUNT(*) AS CountOf
      FROM dbo.myDB
     WHERE Address IS NOT NULL
  GROUP BY Address
    HAVING COUNT(*) > 1

我试过

SELECT Name, 
       Address ...

但是回来时出现错误

谢谢,

4 个答案:

答案 0 :(得分:2)

如果您要将名称添加为列,则必须将其添加到group by ,或将其包含在汇总功能中:< / p>

SELECT
Address, Name, COUNT(*) AS CountOf
FROM dbo.myDB
WHERE Address IS NOT NULL
GROUP BY Address, Name
HAVING COUNT(*)>1

答案 1 :(得分:1)

您必须将字段Name添加到SELECT和GROUP BY

SELECT
Address, Name, COUNT(*) AS CountOf
FROM dbo.myDB
WHERE Address IS NOT NULL
GROUP BY Address, Name
HAVING COUNT(*)>1

答案 2 :(得分:-1)

SELECT
Address, MAX(Name) NAME, COUNT(*) AS CountOf
FROM dbo.myDB
WHERE Address IS NOT NULL
GROUP BY Address
HAVING COUNT(*)>1

SELECT
Address, MIN(Name) NAME, COUNT(*) AS CountOf
FROM dbo.myDB
WHERE Address IS NOT NULL
GROUP BY Address
HAVING COUNT(*)>1

答案 3 :(得分:-3)

您尝试访问的表格如何?如果您没有字段名称,可以在查询中调用它 编辑:我没看到小组。如果你有一个group by子句,那么select上的每个字段也必须在组中(除了像avg这样的公式,总和......)