【问题标题】:Unable to get total count of users in the user group per month in SQL Server无法在 SQL Server 中获取每月用户组中的用户总数
【发布时间】:2013-12-17 11:51:03
【问题描述】:

我有四个不同的表useruser_groupuser_and_groupsactions

可以从user_group 添加/删除用户。所以一个user_group 可能包含许多用户。表user_and_groupsuseruser_group 之间的关系表。操作表包含用户添加/删除到user_groupaction(即添加/删除)执行日期的记录操作。因此,每个月 user_group 中的用户数可能会有所不同。

我正在使用 SQL Server,我想列出每个月用户组中的用户总数。

下表列详细信息:

User Table-> userid, details

user_group Table -> groupid, details

user_and_groups Table -> userid, groupid (many to many)

actions Table -> **actionid, action_type, directid, indirectid, action_date**
where directid is user_group_Id, and indirectid is userid.

**Action table dummy data:**

**|actionid|action_type          |directid|indirectid|action_date|**

|1 |     user_added_in_group   |100      |2001      |1-Jan-2012|

|2 |     user_added_in_group   |100      |2002      |1-Feb-2012|

|3 |     user_added_in_group   |100      |2003      |1-March-2012|

|4 |     user_removed_From_group |100    |2001      |1-March-2012|

|5 |     user_added_in_group    |100     |2004      |1-April-2012|

|6 |     user_added_in_group    |100     |2001      |1-April-2012|

上表中的通知 userid=2001 是在 1 月份添加的,然后在 3 月份删除,然后在 4 月份重新添加。

我想要 user_group_id = 100 每月的输出 - 用户总数。

**Expected output:**

+------+-------+-------+

| Year | Month | Count |

+------+-------+-------+

| 2011 | 12    | 0     |

| 2012 | 01    | 1     |

| 2012 | 02    | 2     |

| 2012 | 03    | 2     |

| 2012 | 04    | 4     |

我无法为此编写 SQL 查询。有人写过同样的查询吗?

/************ ******开始编辑*************/**

我想我已经很接近答案了,所以在这里发布查询:

    DECLARE @countOfUsersInGroup INT = 223 
    --(this count is got from user_and_groups relation ship table. which will give count of users in one group at current date)

-- but we need each month's user count in user group - do following


DECLARE  @addRemoveActionsPerMonth TABLE (id INT IDENTITY ,cyear INT, cmonth INT, addActionsInMonth INT, removedActionsInMonth INT, sumOfAddNRemovedActionsInMonth INT)
INSERT INTO @addRemoveActionsPerMonth (cyear, cmonth, addActionsInMonth, removedActionsInMonth, sumOfAddNRemovedActionsInMonth)
SELECT *
FROM
(
    SELECT YEAR(action_date) as cyear, MONTH(action_date) as cmonth, 
            COUNT( CASE WHEN action_type like 'user_added_in_group' THEN 1  END ) as addActionsInMonth,
            COUNT( CASE WHEN action_type like 'user_removed_From_group' THEN 1  END ) as removedActionsInMonth,              
            COUNT( CASE WHEN action_type like 'user_removed_From_group' THEN 1  END ) - COUNT( CASE WHEN action_type like 'user_added_in_group' THEN 1  END ) as sumOfAddNRemovedActionsInMonth             
    FROM dbo.[action_fact] 
    WHERE direct_object_id = 100 --(group id)
    AND action_type like 'user_added_in_group' or action_type like 'user_removed_From_group'  -- action is added or removed
    GROUP BY year(action_date), Month(action_date)
)
as innerTable
ORDER BY cyear, cmonth


DECLARE  @cumulativeCountPerMonth TABLE (id INT , cumulativeCount INT)
INSERT INTO @cumulativeCountPerMonth (id, cumulativeCount)

    SELECT id, 
        (
            SELECT sum(sumOfAddNRemovedActionsInMonth)
            FROM @addRemoveActionsPerMonth as innerTab 
            WHERE innerTab.id >= outerTab.id 
        ) as cumulativeCount
    FROM @addRemoveActionsPerMonth as outerTab



SELECT  actionsData.id, cyear, cmonth, addActionsInMonth, removedActionsInMonth, sumOfAddNRemovedActionsInMonth, cumulativeCount,
        @countOfUsersInGroup + cumulativeCount as actualUserCountInMonth
FROM @addRemoveActionsPerMonth as actionsData
INNER JOIN @cumulativeCountPerMonth as cumulativeData ON actionsData.id = cumulativeData.id

【问题讨论】:

  • 很好的问题,但是:1. 包括 DDL(表定义)和示例数据,以及 2. 显示您尝试过的内容。
  • 假设在 2013 年 10 月末=> 有 223 个用户。在 OCT 中执行的操作是:添加 4 个,删除 1 个,因此 9 月底的计数将是 220 (223-4+1)=>220 九月份执行的操作是:添加 1 个,删除 2 个,(220-1+2) => 8 月底的 221 个用户 8 月份执行的操作是:添加 3 个,删除 4 个,(220-3+4)=>7 月底有 222 个用户,依此类推......每个月末我都需要用户存在于用户组中。还有一种特殊情况我需要处理:假设 user:1 是在 8 月添加的,然后在同月删除,并在同月重新添加。这将仅计为一个用户
  • 不完全一样,但看看stackoverflow.com/questions/808356/…是否对你有帮助。

标签: sql-server


【解决方案1】:

这个怎么样?

SELECT YEAR (ACTION_DATE), MONTH(ACTION_DATE), COUNT(*)
FROM TABLE_ACTION
GROUP BY YEAR(ACTION_DATE), MONTH(ACTION_DATE);

【讨论】:

  • 感谢 Darshan,但这只会给出每个月的操作数,而不是每个月用户组中存在的用户。
  • 您想显示不存在数据的月份和年份吗?如果是,那么范围是多少?
  • 假设在 2013 年 10 月末=> 有 223 个用户。在 OCT 中执行的操作是:添加 4 个,删除 1 个,因此 9 月底的计数将是 220 (223-4+1)=>220 九月份执行的操作是:添加 1 个,删除 2 个,(220-1+2) => 8 月底的 221 个用户 8 月份执行的操作是:添加 3 个,删除 4 个,(220-3+4)=>7 月底有 222 个用户,依此类推......每个月末我都需要用户存在于用户组中。还有一种特殊情况我需要处理:假设 user:1 是在 8 月添加的,然后在同月删除,并在同月重新添加。这将仅计为一个用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多