SQL Server基于列值查询SUM

时间:2018-03-21 17:35:55

标签: sql sql-server

我有如下表:

fld_bank_cash_particular_id  fld_cr_dr  fld_account_id  fld_amount
1                            Dr         26              1000
2                            Dr         26              2000
3                            Dr         26              3000
4                            Cr         26              4000
5                            Dr         26              5000
6                            Cr         26              6000
7                            Dr         26              7000
8                            Dr         26              8000
9                            Dr         26              9000
10                           Cr         26              10000
11                           Dr         27              1000
12                           Dr         27              2000
13                           Dr         27              3000
14                           Cr         27              4000
15                           Dr         27              5000
16                           Cr         27              6000
17                           Dr         27              7000
18                           Dr         27              8000
19                           Dr         27              9000
20                           Cr         27              1000

我想要所有金额的SUM(),列值为'Dr'作为付款,SUM()为所有金额,列值为'Cr'作为收据[结果应显示AccountId明智的付款和收据]。相同的输出如下:

AccountId   Payments    Receipts
26          35000       20000
27          35000       20000

目前我的结果低于结果:

AccountId   Payments    Receipts
26          0           20000
26          35000       0
27          0           20000
27          35000       0

3 个答案:

答案 0 :(得分:1)

试试这个,它应该有效! 它简单明了!

  SELECT 
      DR.AccountId
     ,Payments
     ,Receipts
    FROM
    (
     (SELECT 
         fld_account_id AccountId
         ,SUM(fld_amount) Payments
        FROM table_name
        WHERE fld_cr_dr = 'Dr'
        GROUP BY fld_account_id) DR
     INNER JOIN
     (SELECT 
         fld_account_id AccountId
         ,SUM(fld_amount) Receipts
        FROM table_name
        WHERE fld_cr_dr = 'Cr'
        GROUP BY fld_account_id) CR ON DR.AccountId = CR.AccountId
    )

答案 1 :(得分:0)

将“TestTable”替换为您的表格:

SELECT DISTINCT AccountID, COALESCE(SumPay, 0) AS Payments, COALESCE(SumRec, 0) AS Receipts
FROM (SELECT fld_account_id AS AccountID
    , (SELECT SUM([fld_amount]) FROM TestTable WHERE fld_cr_dr = 'Dr' AND fld_account_id = t.fld_account_id) AS SumPay
    , (SELECT SUM([fld_amount]) FROM TestTable WHERE fld_cr_dr = 'Cr' AND fld_account_id = t.fld_account_id) AS SumRec
FROM TestTable t) AS tmp

...可能不是最有效的,因为它运行重复的查询,但除非你每分钟运行一百次和/或你的表有数百万条记录,否则应该没问题。

答案 2 :(得分:-1)

SELECT tam.fld_account_id AS [Account Id], 
MAX(CASE WHEN tbcep.fld_cr_dr = 'Dr' THEN SUM(ISNULL(fld_amount,0)) ELSE 0 
END) AS Payments,
MAX(CASE WHEN tbcep.fld_cr_dr = 'Cr' THEN SUM(ISNULL(fld_amount,0)) ELSE 0 
END) AS Receipts
FROM tbl_bank_cash_entry_particulars tbcep
INNER JOIN tbl_bank_cash_entry tbce 
ON tbce.fld_bank_cash_entry_id = tbcep.fld_bank_cash_entry_id 
AND tbce.fld_fy_id=1 AND tbce.fld_is_active=1 AND tbce.fld_is_delete=0
LEFT JOIN tbl_account_master tam 
ON tam.fld_account_id = tbcep.fld_account_id 
AND tam.fld_is_active=1 AND tam.fld_is_delete=0
WHERE tam.fld_account_group_id=36 
AND tbcep.fld_is_active=1 AND tbcep.fld_is_delete=0
            GROUP BY tam.fld_account_id

USE MAX仅选择最大值。所以这种方法将为您提供单行输出