简体   繁体   中英

How to get data in that form using SQL Query

I have stored data in one table 't' in which I have all year data of user what I have . In result of query I want the total number of new users as per year.

Example:

year , count
1991   360
1992   640
2000   2000

Result required:

Year, NewUsers
1991  360
1992  280 (640-360)
2000  1720

You could define a variable to do this:

select
    `year`,
    @lastval := `count` - @lastval as newusers
from yourtable
cross join (select @lastval := 0) a
order by `year`

See demo in SQLFiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM