简体   繁体   English

如何使用SQL查询以该形式获取数据

[英]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 . 我已经将数据存储在一个't'表中,其中存储了我所拥有的用户的全年数据。 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. 请参阅SQLFiddle中的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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