SQL QUERY COUNT然后添加

时间:2017-11-08 13:43:48

标签: sql sql-server count add

我想编写一个查询来返回表中的所有记录, 如果特定列value低于100,则add +5为值

1 个答案:

答案 0 :(得分:1)

每个订单的sum值都需要。根据您的原始问题orderamount <100,然后添加5

    WITH cte
AS (
    SELECT OrderID
        ,OrderDate
        ,Customer
        ,ProductAmount
        ,sum(orderamount) orderamount
    FROM mytable
    GROUP BY OrderID
        ,OrderDate
        ,Customer
        ,ProductAmount
    )
SELECT OrderID
    ,OrderDate
    ,Customer
    ,ProductAmount
    ,CASE 
        WHEN orderamount < 100
            THEN orderamount + 5
        ELSE orderamount
        END Orderamount
FROM cte