我有这个用户表:
id(int) | created (datetime)
我正在显示以下服务器的统计信息: - 你选择一个日期 - 您可以选择是否要显示每日,每周或每月统计信息(请参阅查询)
我通过这样做来使查询非常静态:
WEEK(DATE_ADD(" + date + ", INTERVAL -2 WEEK) // Shows how many users that has been created from two weeks before current date to current date
现在我希望应该查看统计信息的管理员能够选择他想要显示统计信息的时间。 所以我需要使查询动态化。
以下是我所做的原始静态查询:
String sQuery = "SELECT " +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created) as totalNumberOfCreatedUsers, " +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(" + date + ") = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysNumberOfCreatedUsers, " +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -1 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusOneNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -2 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusTwoNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -3 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusThreeNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -4 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusFourNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -5 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusFiveNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -6 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusSixNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -7 DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusSevenNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE WEEK(" + date + ") = WEEK(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisWeeksNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE WEEK(DATE_ADD(" + date + ", INTERVAL -1 WEEK)) = WEEK(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisWeeksMinusOneNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE WEEK(DATE_ADD(" + date + ", INTERVAL -2 WEEK)) = WEEK(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisWeeksMinusTwoNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE WEEK(DATE_ADD(" + date + ", INTERVAL -3 WEEK)) = WEEK(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisWeeksMinusThreeNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE WEEK(DATE_ADD(" + date + ", INTERVAL -4 WEEK)) = WEEK(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisWeeksMinusFourNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(" + date + ") = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -1 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusOneNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -2 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusTwoNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -3 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusThreeNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -4 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusFourNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -5 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusFiveNumberOfCreatedUsers," +
" (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE MONTH(DATE_ADD(" + date + ", INTERVAL -6 MONTH)) = MONTH(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisMonthsMinusSixNumberOfCreatedUsers" +
"";
e.g: 我可以做的一个解决方案(java):(但这会创建一个大型查询,我想要一个更有效的方法来做到这一点)(顺便说一下,这个例子不是工作代码,只是我想到的东西给你和想法什么解决方案可能是)
管理员选择查看20天/周/月
String addToQuery = "";
int daysWeeksMonthsToLookBack = inputFromUser // inputFromUser = 20 in this example
for(int i=0; i<daysWeeksMonthsToLookBack; i++){
addToQuery += " (SELECT count(statistic_customer_created.id) FROM statistic_customer_created WHERE DAYOFYEAR(DATE_ADD(" + date + ", INTERVAL -" + i + " DAY)) = DAYOFYEAR(statistic_customer_created.created) AND YEAR(" + date + ") = YEAR(statistic_customer_created.created)) as thisDaysMinusFiveNumberOfCreatedUsers," +
}
有人建议我如何更有效地做到这一点? 提前致谢
答案 0 :(得分:1)
我会使用像
这样的范围SELECT count(s.id)
FROM statistic_customer_created s
WHERE s.created >= {date calculated in Java}
通过获取这些日期的不同,您可以获得1到2天前创建的所有日期。
过去7天内创建的数量与上周创建的数量应相同。