简体   繁体   English

基于列值的 SUM SQL Server 查询

[英]SQL Server query for SUM based on column value

I have table like below:我有如下表:

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

I want SUM() of all amount with column value 'Dr' as Payments and SUM() of all amount with column value 'Cr' as Receipts[Result should display AccountId wise Payments and Receipts].我想要列值为“Dr”的所有金额的 SUM() 作为付款和列值为“Cr”的所有金额的 SUM() 作为收据 [结果应显示 AccountId 明智的付款和收据]。 Same Output like below:相同的输出如下:

AccountId   Payments    Receipts
26          35000       20000
27          35000       20000

Currently i'm getting below result:目前我得到以下结果:

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

Try this, it should work!试试这个,它应该有效! It is easy and clear!简单明了!

  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
    )

Replace "TestTable" with your table:用您的表格替换“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

...may not be the most efficient, as it runs duplicate queries, but unless you're running it a hundred times a minute and/or your table is millions of records long, it should be ok. ...可能不是最有效的,因为它运行重复查询,但除非您每分钟运行一百次和/或您的表有数百万条记录,否则应该没问题。

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 to select only the max value. USE MAX 仅选择最大值。 so this approach will give you output in a single row所以这种方法会给你单行输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM