当另一列='Accepted'(SQL)时如何求和

如果另一栏符合要求,我目前正在寻找一栏的总和。

在这种情况下,如果[Status] ='Accepted'货件,我想对[Weight]的总和进行汇总。我到处都看过了,没有取得太大进展。这是我尝试过的一个示例:

CASE WHEN [Status:  Accepted, Pending, Rejected] = 'ACCEPTED' 
        THEN SUM(([Weight (lbs)]))  END ) AS ACCEPT_WEIGHT
评论
  • 涂鸦
    涂鸦 回复

    The case is the argument to the sum(), like this:

    SUM(CASE WHEN [Status: Accepted, Pending, Rejected] = 'ACCEPTED' THEN [Weight (lbs)] 
        END) AS ACCEPT_WEIGHT
    

    If you want to ensure that the value is never NULL, then use an ELSE clause:

    SUM(CASE WHEN [Status: Accepted, Pending, Rejected] = 'ACCEPTED' THEN [Weight (lbs)] 
             ELSE 0
        END) AS ACCEPT_WEIGHT
    
  • 淡_然
    淡_然 回复

    您需要在大小写条件之外使用sum(),例如:

    sum( CASE WHEN [Status:  Accepted, Pending, Rejected] = 'ACCEPTED' 
        THEN [Weight (lbs)] ELSE 0  END ) AS ACCEPT_WEIGHT
    
  • 芬芬
    芬芬 回复

    可能是这样的:

    SELECT SUM(CASE WHEN T.[Status] = 'ACCEPTED' THEN T.[Weight (lbs)] ELSE 0  END ) AS ACCEPT_WEIGHT FROM YOURTABLE AS T
    

    希望这可以帮助!