WITH MonthlyLogins AS (
SELECT
Username,
LicenseType,
DATE_TRUNC('month', LoginTime) AS LoginMonth,
COUNT(DISTINCT DATE(LoginTime)) AS UniqueLoginDays
FROM your_table
WHERE LoginTime >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '3 month')
GROUP BY Username, LicenseType, LoginMonth
)
SELECT
Username,
LicenseType,
SUM(UniqueLoginDays) AS TotalUniqueLoginDays
FROM MonthlyLogins
GROUP BY Username, LicenseType
ORDER BY TotalUniqueLoginDays DESC;
说明:
WITH MonthlyLogins AS (...):此项会定义一个名为 MonthlyLogins 的通用表表达式 (CTE),用于计算每个月内每个用户和许可类型的唯一登录天数。
SELECT ... FROM your_table WHERE LoginTime >= ...:此项用于从表中选择相关数据,并过滤出过去 3 个月内的登录事件。
DATE_TRUNC('month', LoginTime) AS LoginMonth:此项会从 LoginTime 列中提取一个月的月初登录并将其别名命名为 LoginMonth。
COUNT(DISTINCT DATE(LoginTime)) AS UniqueLoginDays:此项会计算每个月内的不同登录日期,为我们提供唯一登录天数。
GROUP BY Username, LicenseType, LoginMonth:此项按用户名、许可类型和登录月份对结果进行分组。
SELECT ... FROM MonthlyLogins GROUP BY Username, LicenseType:此项会选择用户名、许可类型,还会对所有月份内每个用户和许可类型的 UniqueLoginDays 进行求和。
ORDER BY TotalUniqueLoginDays DESC:此项会根据唯一登录总天数,按降序对结果进行排序。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-09-05。"],[],[]]