MySQL data statistics based on stored procedure statistics template by day, week, month

Source: Internet
Author: User

The stored procedure Developer_count is based on the incoming parameter SearchType decision is to use that query method, the other parameters contained in this stored procedure is {start time: Startime, End time: Endtime}

[HTML]View PlainCopy
  1. CREATE PROCEDURE Developer_count
  2. (
  3. SearchType int,
  4. StartTime varchar (64),
  5. EndTime varchar (64)
  6. )
  7. BEGIN
  8. /* Define Variable days */
  9. declare day_num int;
  10. If searchtype = 1 Then
  11. /* Data query this week */
  12. Select count (d.acct_id), d.acct_old_time from developer D where 1=1 and Date_sub (Curdate (), INTERVAL 7 day) &L  t;= Date (d.acct_old_time) GROUP by D.acct_old_time;
  13. End If;
  14. If searchtype = 2 Then
  15. /* Data query this month */
  16. Select count (d.acct_id), d.acct_old_time from developer D where 1=1 and Date_sub (Curdate (), INTERVAL-day) &  lt;= Date (d.acct_old_time) GROUP by D.acct_old_time;
  17. End If;
  18. If searchtype = 3 Then
  19. /* Data query for the last three months */
  20. Select count (d.acct_id), Date_format (D.acct_old_time, '%x year-%v week ') as weeks from developer D where 1=1 and Date_sub (Curdate (), INTERVAL Day)  <= Date (d.acct_old_time) GROUP by weeks;
  21. End If;
  22. If searchtype = 4 Then
  23. /* Data statistics by Month */
  24. Select DateDiff (StartTime, endTime) into Day_num;
  25. If Day_num <=7 Then
  26. Select count (d.acct_id), d.acct_old_time from developer D where 1=1 and Date_sub (Curdate (), INTERVAL Day_num Day)  <= Date (d.acct_old_time) GROUP by D.acct_old_time;
  27. End If;
  28. If Day_num >7 && day_num <=
  29. Select count (d.acct_id), d.acct_old_time from developer D where 1=1 and Date_sub (Curdate (), INTERVAL Day_num Day)  <= Date (d.acct_old_time) GROUP by D.acct_old_time;
  30. End If;
  31. If Day_num >30 && day_num <=
  32. Select count (d.acct_id), Date_format (D.acct_old_time, '%x year-%v week ') as weeks from developer D where 1=1 and Date_sub (Curdate (), INTERVAL Day_num Day)  <= Date (d.acct_old_time) GROUP by weeks;
  33. End If;
  34. End If;
  35. End

Calling a stored procedure method

Call Developer_count (1, ' 2016-06-07 ', ' 2016-06-16 '); Calling a stored procedure

MySQL-based stored procedures for data statistics by day, week, month statistics template

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.