SQL Monthly count and monthly cumulative count

Source: Internet
Author: User

1, simple, monthly statistics quantity

SELECT ' %y-%m '  as Month    COUNT (* as Sum from Pt_user GROUP        bymonth;

2, by the monthly cumulative statistical data

SELECTA.Month,SUM(b.total) as Total from (    SELECTDate_format (Create_date,'%y-%m') as Month,SUM(sum) as Total from (        SELECTCreate_date, Date_format (Create_date,'%y-%m') as Month            , COUNT(*) as sum         fromPt_userGROUP  by Month) PtuserGROUP  by Month) AJOIN (        SELECTDate_format (Create_date,'%y-%m') as Month,SUM(sum) as Total from (            SELECTCreate_date, Date_format (Create_date,'%y-%m') as Month                , COUNT(*) as sum             fromPt_userGROUP  by Month) PtuserGROUP  by Month) b onA.MONTH >=B.MONTHGROUP  byA.MONTHORDER  byA.Month;

3, multi-field monthly cumulative statistics (eg: Cumulative monthly growth per tenant statistics)

SELECTA.ORGAN_ID, A.Month,SUM(b.total) as Total from (    SELECTorgan_id, Date_format (Create_time,'%y-%m') as Month        , SUM(sum) as Total from (        SELECTorgan_id, Create_time, Date_format (Create_time,'%y-%m') as Month            , COUNT(*) as sum         fromPt_userGROUP  by Month) PtuserGROUP  byORGAN_ID,Month) AJOIN (        SELECTorgan_id, Date_format (Create_time,'%y-%m') as Month            , SUM(sum) as Total from (            SELECTorgan_id, Create_time, Date_format (Create_time,'%y-%m') as Month                , COUNT(*) as sum             fromPt_userGROUP  by Month) PtuserGROUP  byORGAN_ID,Month) b ona.organ_id=b.organ_id andA.MONTH >=B.MONTHGROUP  byA.MONTHORDER  byA.ORGAN_ID, A.Month;

SQL Monthly count and monthly cumulative count

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.