除了我在查询中指定的列之外,我必须在输出中有以下列
过去7天内每个userID的(nativeprodID)计数
过去15天内每个userID的(nativeProdID)计数
过去30天内每个用户ID的(nativeprodID)计数
SELECT userid,nativeprodID,nativeVersion,nativeVersionName,
MAX(StartTime)AS Last_Used_TimeStamp,
来自
[ProdTable]
在哪里NativeProd ='AAA'
AND GlobalPod ='AAA'
GROUP BY 1,2,3,4
限制10
我在考虑使用交叉连接,但还有其他更简单的技术吗?
答案 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,
FROM [ProdTable]
WHERE NativeProd ='AAA' AND GlobalPod='AAA'
GROUP BY 1,2,3,4
LIMIT 10