SQL Server中的COUNT和GROUP BY

时间:2012-03-07 22:58:22

标签: sql sql-server sql-server-2008 tsql

我有这个查询并且它做了几个连接,但我想做一个Count和它现在的方式它给每个人一个计数1。我做错了什么才能实现这一点?谢谢!

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  COUNT(Country.Name) As num,
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID
WHERE
  EXISTS
  (SELECT
    NULL
   FROM
    CountryUser CU2
   WHERE
    CU2.[Foreign ID] = CountryUser.[Foreign ID]
    AND CU2.[User ID] <> CountryUser.[User ID])
    GROUP BY UserColder.ContactName, CountryUser.[User ID], Country.Name, Country.ID

编辑:

我的数据如下:

Bob   United States
Tom   United States
Steve United Stated

Frank Canada
Billy Canada

Lou   China

所以在美国旁边应该是3,加拿大2和中国1。

2 个答案:

答案 0 :(得分:2)

要计算每个国家/地区的用户数,window function是一种可能性。可能看起来像这样:

SELECT u.ContactName
      ,cu.[User ID]
      ,c.Name
      ,c.ID
      ,count(*) OVER (PARTITION BY c.ID) AS users_in_this_country
FROM   dbo.Country     AS  c
JOIN   dbo.CountryUser AS cu ON c.ID = cu.[Foreign ID]
JOIN   dbo.UserColder  AS  u ON cu.[User ID] = u.ID
WHERE  EXISTS (
    SELECT *
    FROM   CountryUser AS cu2
    WHERE  cu2.[Foreign ID] = cu.[Foreign ID]
    AND    cu2.[User ID] <> cu.[User ID])
    )

我还使用表别名来缩短语法(不更改功能)并删除GROUP BY子查询中的EXISTS,因为它没有函数。


如何按窗口函数的结果进行过滤?

回复评论:在您的情况下,您已使用EXISTS子句实现了此特定过滤器。

通常,要根据窗口函数的结果进行过滤,请使用子查询或CTE,如下所示:

;With cte AS (
   < query from above >
   )
SELECT *
FROM   cte
WHERE  users_in_this_country > 1

答案 1 :(得分:0)

您需要在外部查询的select部分中创建一个子查询,并将其连接到外部查询。例如,它应该看起来像这样。希望这会有所帮助。

SELECT
  UserColder.ContactName,
  CountryUser.[User ID],
  (SELECT COUNT(C2.Name) FROM dbo.Country C2 INNER JOIN dbo.CountryUser CU2
    ON C2.ID = CU2.[Foreign ID]
    WHERE UserColder.ID = CU2.[User ID])  As num,
  Country.Name,
  Country.ID
FROM
  dbo.Country
  INNER JOIN dbo.CountryUser
    ON Country.ID = CountryUser.[Foreign ID]
  INNER JOIN dbo.UserColder
    ON CountryUser.[User ID] = UserColder.ID