在sqlite中向该表添加增长列

时间:2018-10-16 14:55:46

标签: sql sqlite

我在sqlite3数据库中有此表Sales

Name    Sales       Date
"Vent"  "75.998"    "2014-12-31"
"Vent"  "153.988"   "2015-12-31"
"Vent"  "180.678"   "2016-12-31"
"Vent"  "372.819"   "2017-12-31"
"DBG"   "4046.0"    "2014-12-31"
"DBG"   "4454.0"    "2015-12-31"
"DBG"   "4238.0"    "2016-12-31"
"DBG"   "4371.0"    "2017-12-31"

我想在此表中添加更多列以显示销售增长。新表将如下所示:

Name    Sales       Date          Year1_Growth      Year2_Growth        Year3_Growth
"Vent"  "75.998"    "2014-12-31"  null              null                null 
"Vent"  "153.988"   "2015-12-31"  153.988/75.998    null                null
"Vent"  "180.678"   "2016-12-31"  180.678/153.988   180.678/75.998      null
"Vent"  "372.819"   "2017-12-31"  372.819/180.678   372.819/153.988     372.819/75.998
"DBG"   "4046.0"    "2014-12-31"  null              null                null 
"DBG"   "4454.0"    "2015-12-31"  4454/4046         null                null 
"DBG"   "4238.0"    "2016-12-31"  4238/4454         4238/4046           null 
"DBG"   "4371.0"    "2017-12-31"  4371/4238         4371/4454           4371/4046          

关于如何开始使用sql代码的任何提示?

3 个答案:

答案 0 :(得分:3)

如果您使用的是Sqlite 3.25或更高版本,则可以消除联接,这可以使用窗口函数很容易地完成:

SELECT name
     , sales
     , date
     , sales / lag(sales, 1) OVER past_sales AS Year1_Growth
     , sales / lag(sales, 2) OVER past_sales AS Year2_Growth
     , sales / lag(sales, 3) OVER past_sales AS Year3_Growth
FROM sales
WINDOW past_sales AS (PARTITION BY name ORDER BY date)
ORDER BY name DESC, date;

给予

name        sales       date        Year1_Growth  Year2_Growth  Year3_Growth
----------  ----------  ----------  ------------  ------------  ------------
Vent        75.998      2014-12-31  null          null          null        
Vent        153.988     2015-12-31  2.0262112160  null          null        
Vent        180.678     2016-12-31  1.1733251941  2.3774046685  null        
Vent        372.819     2017-12-31  2.0634443595  2.4210912538  4.9056422537
DBG         4046.0      2014-12-31  null          null          null        
DBG         4454.0      2015-12-31  1.1008403361  null          null        
DBG         4238.0      2016-12-31  0.9515042658  1.0474542758  null        
DBG         4371.0      2017-12-31  1.0313827277  0.9813650651  1.0803262481

编辑:转储示例表模式和数据:

CREATE TABLE sales(name TEXT, sales REAL, date TEXT);
INSERT INTO sales VALUES('Vent',75.99800000000000466,'2014-12-31');
INSERT INTO sales VALUES('Vent',153.98799999999999954,'2015-12-31');
INSERT INTO sales VALUES('Vent',180.67799999999999727,'2016-12-31');
INSERT INTO sales VALUES('Vent',372.81900000000001683,'2017-12-31');
INSERT INTO sales VALUES('DBG',4046.0,'2014-12-31');
INSERT INTO sales VALUES('DBG',4453.9999999999999999,'2015-12-31');
INSERT INTO sales VALUES('DBG',4238.0000000000000001,'2016-12-31');
INSERT INTO sales VALUES('DBG',4370.9999999999999999,'2017-12-31');

答案 1 :(得分:2)

您可以使用Jackson

join

答案 2 :(得分:2)

我们可以通过一系列自我联接来实现这一目标。请注意,在下面的查询中,我实际上打印出了销售数字的文本比率。这应该可以帮助您确认查询中的逻辑是正确的。如果您想要实际的数字比例,则只需使用例如s1.Sales / s2.Sales,而不是字符串串联。

SELECT
    s1.Name,
    s1.Sales,
    s1.Date,
    s1.Sales || '/' || s2.Sales AS first,      -- use s1.Sales / s2.Sales
    s1.Sales || '/' || s3.Sales AS second,     -- for actual ratio
    s1.Sales || '/' || s4.Sales AS third
FROM Sales s1
LEFT JOIN Sales s2
    ON s1.Name = s2.Name AND
       CAST(SUBSTR(s1.Date, 1, 4) AS int) = CAST(SUBSTR(s2.Date, 1, 4) AS int) + 1
LEFT JOIN Sales s3
    ON s1.Name = s3.Name AND
       CAST(SUBSTR(s1.Date, 1, 4) AS int) = CAST(SUBSTR(s3.Date, 1, 4) AS int) + 2
LEFT JOIN Sales s4
    ON s1.Name = s4.Name AND
       CAST(SUBSTR(s1.Date, 1, 4) AS int) = CAST(SUBSTR(s4.Date, 1, 4) AS int) + 3
ORDER BY
    s1.Name, s1.Date;

enter image description here

Demo