A SQL that counts past data.

Source: Internet
Author: User
Tags idate

Today, a friend made a scene: A store's income record table, every day is recorded;

Date Daily income
07/11 100
07/12 102
07/13 103
07/14 101

Now do a statistic that users want to see their own income statistics

Date Daily income
07/11 100
07/12 202
07/13 305
07/14 50W

CREATE TABLE IF  not EXISTS' income ' (' ID ')int( One) not NULL, ' idate ' dateDEFAULT NULL,  ` Money`int( One)DEFAULT NULL,  PRIMARY KEY(' id '),UNIQUE KEY' Idate ' (' Idate ')) ENGINE=MyISAMDEFAULTCHARSET=Latin1;

Select a.idate,sum(b. money as numfrom as left    join as on B.idate <= Group by  Order by

SELECTID, Idate, (@rowNum:= @rowNum +  Money) asTotal fromIncome, (SELECT(@rowNum:=Ifnull ((SELECT sum( Money) fromIncomeWHEREId<=0) ,0)) bORDER  byID limit0, -

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.