【发布时间】:2021-08-17 02:38:43
【问题描述】:
考虑具有 2 列的表格,每次用户登录时都会有一个新行。(大约 1000 万行,并且可以有重复,因为用户每月可以多次登录)
| Sign in Month | MemberID |
|---|---|
| 2020-10 | 1000000 |
| 2020-12 | 1000001 |
现在要找出每个月的唯一用户,我可以使用以下查询。
Select Sign_in_Month, count(distinct(memberid)) as unique_users from table group by Sign_in_Month
但我想将第一个查询中的不同计数进一步分解为 2 个同类群组,其中用户要么在过去 3 个月前登录,要么没有登录。 (2020 年 10 月为 2020 年 7 月至 2020 年 9 月,2020 年 9 月为 2020 年 6 月至 8 月)
我曾经通过 DAX 在 powerBI 中执行此操作,这对于过滤上下文很容易,但我不确定如何在 SQL 中实现。
想要的结果
| Sign in Month | Total Unique Users | Unique Users who signed in in the last 3 months | Unique Users who did not sign in in the last 3 months |
|---|---|---|---|
| 2020-10 | 4000 | 3000 | 1000 |
| 2020-11 | 5000 | 2500 | 2500 |
| 2020-12 | 3500 | 1500 | 2000 |
最后 2 列应与第二列相加。
我如何创建某种指标,说明会员在过去 3 个月内购物的地点参考该特定月份?
谢谢
【问题讨论】:
-
用您正在使用的数据库标记您的问题。
标签: sql sql-server dax