SQLite中的列错误

时间:2018-07-02 19:08:10

标签: sqlite

我使用了CASE函数来创建新列。当我尝试使用新列时,出现以下列错误:

  

没有这样的列:price_each

SELECT  o1.orderid,
        productid,
        shipcountry,
        quantity,
        unitprice,
        discount,
        case
            WHEN discount > 0
                THEN unitprice - (unitprice * discount)
            ELSE unitprice
        END AS price_each,
        quantity * price_each AS ttl
    FROM orders o1 JOIN OrderDetails o2
        ON o1.orderid = o2.orderid

1 个答案:

答案 0 :(得分:0)

请检查以下内容:

SELECT  o1.orderid,
        productid,
        shipcountry,
        quantity,
        unitprice,
        discount,
        case
            WHEN discount > 0
                THEN unitprice - (unitprice * discount)
            ELSE unitprice
        END AS price_each,
        quantity * (SELECT price_each) AS ttl
    FROM orders o1 JOIN OrderDetails o2
        ON o1.orderid = o2.orderid

或者可以通过以下方式完成:

SELECT ord_id,productid,shipcountry,quantity,unitprice,discount,price_each, (price_each * quantity) AS ttl FROM 
(SELECT  o1.orderid AS ord_id,
        productid,
        shipcountry,
        quantity,
        unitprice,
        discount,
        case
            WHEN discount > 0
                THEN unitprice - (unitprice * discount)
            ELSE unitprice
        END AS price_each
    FROM orders o1 JOIN OrderDetails o2
        ON o1.orderid = o2.orderid)
AS VR_table