SQL Select case

后端 未结 3 1225
梦谈多话
梦谈多话 2021-01-18 16:34

I have the following sql tables

oitems table

    +---------+-----------+----------+
    | orderid | catalogid | numitems |
    +---------+-----------+------         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 17:17

    Something like:

    SELECT
         oi.catalog_id,
         SUM(CASE
                WHEN ocardtype in ('Paypal','Sofort') THEN numitems
                WHEN ocardtype in ('Mastercard','Visa') and odate is not null THEN numitems
                ELSE 0 END) as numitems,
         SUM(CASE
                WHEN ocardtype is null then numitems
                WHEN ocardtype in ('Mastercard','Visa') and odate is null THEN numitems
                ELSE 0 END) as ignoreditems
    FROM
       oitems oi
          inner join
       Orders o
          on
             oi.orderid = o.orderid
    GROUP BY
       oi.catalog_id
    

    (Assuming that wherever you've used the word "empty" in your narrative, you mean the column is NULL)

提交回复
热议问题