计算周期,计算已计算的周期

时间:2017-05-10 08:24:11

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

我有我的疑问:

SELECT UserGroupCode,COUNT(UserGroupCode) AS [CountofCycle]
FROM Users.GroupCycles
GROUP BY UserGroupCode;

这告诉我:

UserGroupCode CountofCycles
1                   1
4                   1
5                   1
6                   2  (gone into 2nd cycle)
7                   1
8                   1
9                   1
10                  1
11                  1
12                  1
13                  1
14                  1
15                  1
16                  1
17                  1
18                  1
19                  1

当我尝试计算countofcycle = 1的总用户组时

SELECT Count(t.CountOfCycle) AS 'totalgroups'
FROM 
    (SELECT CreateDate, COUNT(userGroupCode) AS [CountofCycle] 
     FROM Users.GroupCycles 
    GROUP BY CreateDate,UserGroupCode)t
WHERE CountofCycle=1

我得到结果= 18应该是16,如果我从SELECT和GROUP BY语句中删除CreateDate我可以得到正确数量的CountofCycles, 当我将条件改为CountofCycle = 2或> 1时,它显示我0

显示带有周期>的UserGroups有什么问题? 1 ??? ??? ??

这是我的查询过滤掉onCreateDate,在第二个表中i UNION第一个,我不能使用CreateDate,因为它扰乱了我的查询结果

SELECT Count(t.CountOfCycle) AS 'total groups' 
FROM 
    (SELECT COUNT(userGroupCode) AS [CountofCycle], CreateDate
     FROM users.GroupCycles GROUP BY userGroupCode,CreateDate)t
WHERE t.CountOfCycle=1 AND t.CreateDate Between '03/16/2017' AND '04/25/2017'


UNION ALL

SELECT Count(t.CountOfCycle) AS 'group on date2'
FROM 
    (SELECT COUNT(userGroupCode) AS [CountofCycle] FROM users.GroupCycles GROUP BY  userGroupCode)t
WHERE t.CountOfCycle=2

3 个答案:

答案 0 :(得分:0)

你可以使用HAVING,应该工作(并且效率更高)

select count(*)
from
(
SELECT CreateDate, COUNT(userGroupCode) AS [CountofCycle] 
FROM Users.GroupCycles 
GROUP BY CreateDate,UserGroupCode
having count(userGroupCode) > 1 -- here is HAVING clause
) x1

答案 1 :(得分:0)

我希望看到HAVING条款而不是WHERE

SELECT UserGroupCode, COUNT(UserGroupCode) [CountofCycle]
FROM [Users].[GroupCycles]
GROUP BY UserGroupCode
HAVING COUNT(UserGroupCode) > 1;

答案 2 :(得分:0)

首先要解决为什么你没有得到你期望的结果,原因很简单,你要比较两个不同的查询,并期望结果是相同的。

考虑这个非常简单的示例数据

UserGroupCode   |   CreateDate
----------------+----------------
    A           |   2017-05-10
    B           |   2017-05-10
    B           |   2017-05-11
    C           |   2017-05-10

您有两条UserGroupCodeB的记录,因此如果您运行:

DECLARE @T TABLE (UserGroupCode CHAR(1), CreateDate DATE)
INSERT @T (userGroupCode, CreateDate) 
VALUES ('A', '2017-05-10'), ('B', '2017-05-10'), ('B', '2017-05-11');

SELECT  UserGroupCode, COUNT(*) AS [Count]
FROM    @T
GROUP BY UserGroupCode
HAVING COUNT(*) = 2;

返回:

UserGroupCode   Count
-------------------------
    B           2

但是,如果您要将CreateDate添加到分组中,“B”将分为两组,每组都计数为1:

DECLARE @T TABLE (UserGroupCode CHAR(1), CreateDate DATE)
INSERT @T (userGroupCode, CreateDate) 
VALUES ('A', '2017-05-10'), ('B', '2017-05-10'), ('B', '2017-05-11');

SELECT  UserGroupCode, CreateDate, COUNT(*) AS [Count]
FROM    @T
WHERE   UserGroupCode = 'B'
GROUP BY UserGroupCode, CreateDate;

返回:

UserGroupCode   CreateDate      Count
---------------------------------------
    B           2017-05-10      1
    B           2017-05-11      1

现在,根据您发布的查询,看起来您想知道

  1. 2017年3月16日至2017年4月25日期间只有一条记录的群组数量。
  2. 总共有两条记录的组数。
  3. 为此,请考虑稍大的数据集:

    UserGroupCode   |   CreateDate
    ----------------+----------------
        A           |   2017-04-10
        B           |   2017-04-10
        B           |   2017-05-11
        C           |   2017-01-01
        C           |   2017-01-02
        D           |   2017-04-01
        D           |   2017-04-02
        E           |   2017-01-02
    

    所以在这里。

    • A组总共有一条记录,它在日期范围内
    • B组共有两条记录,在日期范围内,一条不是
    • C组有两条记录,日期范围
    • D组有两条记录,均在日期范围内。
    • E组有一条记录,不在日期范围内

    因此,第一个要求是:

    1. 2017年3月16日至2017年4月25日期间只有一条记录的群组数量。
    2. 我们期望有两个组,A和B,因为C和E在日期范围内没有记录,D有两个。

      第二个我们期望三个组,B,C和D,因为A和E每个只有一个记录。

      您可以使用条件聚合通过单个查询执行此操作。

      DECLARE @T TABLE (UserGroupCode CHAR(1), CreateDate DATE)
      INSERT @T (userGroupCode, CreateDate) 
      VALUES ('A', '2017-04-10'), 
              ('B', '2017-04-10'), ('B', '2017-05-11'), 
              ('C', '2017-01-01'), ('C', '2017-01-02'),
              ('D', '2017-04-01'), ('D', '2017-04-02'),
              ('E', '2017-01-02');
      
      SELECT  TotalGroups = COUNT(CASE WHEN RecordsInPeriod = 1 THEN 1 END),
              GroupOnDate2 = COUNT(CASE WHEN TotalRecords = 2 THEN 1 END)
      FROM    (   SELECT  UserGroupCode,
                          TotalRecords = COUNT(*),
                          RecordsInPeriod = COUNT(CASE WHEN CreateDate >= '20170316'
                                                      AND CreateDate <= '20170425' THEN 1 END)
                  FROM    @T
                  GROUP BY UserGroupCode
              ) AS t;
      

      给出了:

      TotalGroups     GroupOnDate2
      ------------------------------
          2               3