SQLite3基于列创建两次合并行的视图

SQLite3基于列创建两次合并行的视图,sqlite,Sqlite,给一张桌子 symbol|action|quantity|price foo|buy|1|-10.00 foo|sell|1|10.50 bar|buy|1|-50.00 bar|sell|1|50.50 我试图创建一个视图,显示每个符号的利润 我已经尝试了下面的几种变体,使用了多个“orderby”,但没有得到预期的输出 create view if not exists profit_view as select symbol,sum(price * quanity) as profit

给一张桌子

symbol|action|quantity|price
foo|buy|1|-10.00
foo|sell|1|10.50
bar|buy|1|-50.00
bar|sell|1|50.50
我试图创建一个视图,显示每个符号的利润

我已经尝试了下面的几种变体,使用了多个“orderby”,但没有得到预期的输出

create view if not exists profit_view as select symbol,sum(price * quanity) as profit from trans group by symbol;
我想要的是一个显示或接近的视图

symbol|profit|percent
foo|.50|5

假设每个符号只有一个买卖记录,我们可以尝试:

SELECT
    symbol,
    SUM(quantity*price) AS profit,
    100.0 * SUM(quantity*price) /
        ABS(MAX(CASE WHEN action = 'buy' THEN quantity*price END)) AS percent
FROM trans
GROUP BY
    symbol;