开发者

Sqlite query problem

开发者 https://www.devze.com 2023-02-17 00:03 出处:网络
I am having trouble making a query that aims to get the percentage of variation in sales between 2 months.

I am having trouble making a query that aims to get the percentage of variation in sales between 2 months.

I have an example in SQL Server that wor开发者_如何学Goks correctly.

Example:

CREATE TABLE TAB1 ( SELLMONTH1 INT
                  , SELLMONTH2 INT );

INSERT INTO TAB1 VALUES (1000,1250);

SELECT convert(decimal(10,2)
     , (convert(float,(SELLMONTH2 - SELLMONTH1)) / SELLMONTH1) * 100)
FROM TAB1;

Returns me 25.00

How to do that same query in SQLite?

I've tried various ways but always returns 0

Thanks in advance


Try to CAST one of them as FLOAT to avoid integer division (From this thread)


Try this:

SELECT (SELLMONTH2 - SELLMONTH1) * 100.0 / SELLMONTH1
FROM TAB1;

If you specificaly want to cast them as floats (or numeric or whatever), don't use CONVERT but CAST instead:

CAST( sellmonth AS float )
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号