在下面的查询中,我需要包含2个额外的列,用于计算过去1天内每个用户ID的nativeprod(3个本机prods中的每一个--AAA,BBB,CCC)的计数。我将where子句更改为NativeProd IN('AAA','BBB','CCC')和GlobalProd保持不变。 以下添加列名称:
BBB_count_last_1_Day
CCC_count_last_1_Day
选择
用户ID,
nativeprodID,
nativeVersion,
nativeVersionName,
MAX(StartTime)AS Last_Used_TimeStamp,
SUM(IF(DATE(StartTime)> DATE(DATE_ADD(CURRENT_DATE(), - 7,“DAY”)),1,0))AS count_last_7_days,
SUM(IF(DATE(StartTime)> DATE(DATE_ADD(CURRENT_DATE(), - 15,“DAY”)),1,0))AS count_last_15_days,
SUM(IF(DATE(StartTime)> DATE(DATE_ADD(CURRENT_DATE(), - 30,“DAY”)),1,0))AS count_last_30_days,
来自[ProdTable]
WHERE NativeProd ='AAA'和GlobalPod ='AAA'
GROUP BY 1,2,3,4
限制10
我尝试使用nativeprod分区,但我收到一条错误,说“分析表达式中缺少函数”。我的下一个选择是使用连接和匹配用户ID来计算值。还有其他更好的方法可以解决这个问题吗?
答案 0 :(得分:1)
SELECT
userid,
nativeprodID,
nativeVersion,
nativeVersionName,
MAX(StartTime) AS Last_Used_TimeStamp,
SUM(IF(DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -7, "DAY")), 1, 0)) AS count_last_7_days,
SUM(IF(DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -15, "DAY")), 1, 0)) AS count_last_15_days,
SUM(IF(DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -30, "DAY")), 1, 0)) AS count_last_30_days,
-- new columns for last 1 day
SUM(IF(NativeProd = 'AAA' AND DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -1, "DAY")), 1, 0)) AS AAA_count_last_1_Day,
SUM(IF(NativeProd = 'BBB' AND DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -1, "DAY")), 1, 0)) AS BBB_count_last_1_Day,
SUM(IF(NativeProd = 'CCC' AND DATE(StartTime) > DATE(DATE_ADD(CURRENT_DATE() , -1, "DAY")), 1, 0)) AS CCC_count_last_1_Day
FROM [ProdTable]
WHERE NativeProd IN('AAA','BBB','CCC') AND GlobalPod='AAA'
GROUP BY 1,2,3,4
LIMIT 10