SQL - 如何显示总和小于5000的员工

时间:2017-09-20 08:36:54

标签: sql sql-server rdbms

我在这里遇到了一个问题。在total_trans_amount列中,如何检查每个员工的total_trans_amount总和,并显示总共少于5000的员工。根据下图,P0947总共应该有5700个。我想删除由P0947组成的三行数据,因为它超过了5700.请参考我当前的SQL查询和图片。

SELECT DISTINCT b
    ,SUM(trans_amount2) AS [total_trans_amount]
    ,adjust_amount
    ,trans_amount2
    ,transaction_order
    ,reimname_en
    ,currency_code
    ,remark
    ,disease
FROM temp_reim_used
WHERE company_code = 'pdc'
    AND reim_code = 'CLNC_NPL'
    AND a = a
    AND b = b
    AND trans_amount != 0
    AND reimbalance_id = reimbalance_id
    AND (
        (created_date >= {d '2017-01-01' }
            AND created_date <= {d '2017-09-23' })
        )
GROUP BY b
    ,reim_code
    ,adjust_amount
    ,trans_amount2
    ,transaction_order
    ,reimname_en
    ,currency_code
    ,remark
    ,disease
HAVING SUM(trans_amount2) > - 6000
ORDER BY b ASC

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以像这样添加额外的过滤器:

WITH CTE
AS
(
   SELECT b,
     SUM(trans_amount2) as [total_trans_amount]
   FROM temp_reim_used
  where company_code = 'pdc' AND reim_code = 'CLNC_NPL'
  AND a = a AND b = b
  AND trans_amount != 0  AND reimbalance_id = reimbalance_id
  AND ((created_date >= {d '2017-01-01'} AND created_date <= {d '2017-09-23'})) 
   GROUP BY b
)
SELECT 
  b,
  SUM(trans_amount2) as [total_trans_amount], 
  adjust_amount,trans_amount2,transaction_order,
  reimname_en , currency_code, remark, disease
from temp_reim_used
where company_code = 'pdc' AND reim_code = 'CLNC_NPL'
  AND a = a AND b = b
  AND trans_amount != 0  AND reimbalance_id = reimbalance_id
  AND ((created_date >= {d '2017-01-01'} AND created_date <= {d '2017-09-23'})) 
  AND b IN (SELECT b FROM CTE WHERE total_trans_amount >= -5000)
GROUP BY b,reim_code, adjust_amount,trans_amount2,transaction_order, 
reimname_en, currency_code, remark, disease 
having SUM(trans_amount2) >-6000 
order by b asc

此处需要注意的事项:

  • CTE会为您提供每个b的总金额。所以这三行会给你5700。
  • 条件AND b IN (SELECT b FROM CTE WHERE total_trans_amount >= -5000)只会为您提供小于-5000
  • 的行
  • 不需要DISTINCT GROUP BY的方式。

答案 1 :(得分:0)

好好试试这个:

WITH subSource
AS (
    /* your query here */
    )
    ,subSource2 AS (
    SELECT b
        ,total_trans_amount
        ,adjust_amount
        ,trans_amount2
        ,transaction_order
        ,reimname_en
        ,currency_code
        ,remark
        ,disease
        ,sum(total_trans_amount) OVER (PARTITION BY b /*maybe more?*/) AS tempTotal
    FROM subSource
    )
SELECT b
    ,total_trans_amount
    ,adjust_amount
    ,trans_amount2
    ,transaction_order
    ,reimname_en
    ,currency_code
    ,remark
    ,disease
FROM subSource2
WHERE tempTotal <= 5000 /* or just '<' ? */

答案 2 :(得分:0)

在我看来,你写了很多东西&#34;&#34;在您的查询中。 你能看出这个适合你吗?

SELECT * 
FROM (
    SELECT b,
        adjust_amount,
        trans_amount,
        SUM(trans_amount) OVER (PARTITION BY b) AS TOTAL_TRANS_AMOUNT
        transaction_order,
        reimname_en, 
        currency_code, 
        remark, 
        disease
    from temp_reim_used
    where company_code = 'pdc' 
        AND reim_code = 'CLNC_NPL'
        AND trans_amount != 0  
        AND reimbalance_id = reimbalance_id
        AND created_date >= {d '2017-01-01'} AND created_date <= {d '2017-09-23'}
    ) X
WHERE   TOTAL_TRANS_AMOUNT>-5000
ORDER BY b