对 GROUP BY 结果进行分组
Group the GROUP BY results
我有一个问题:
Select
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0))
+ Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
它 returns 23 行的所有账户摘要表明有多少客户必须 支付(-ve) 和 接收(+ve) 每个账户:
Balance
=========
800655.00
1869213.50
-6365.25
1148160.00
145743.70
804225.00
157625.00
66440.00
972950.00
780063.50
646680.75
277761.00
347100.00
-70882.50
-7435.50
431940.00
1319340.00
245685.00
372400.00
158220.00
608108.00
6777029.00
1147920.00
现在我想对这个总结做个总结。支付多少和获得多少累计值。例如:
Summary
===========
-84683.25 //sum of all negative values
19077259.45 //sum of all positive values
我是这样做的:
SELECT SUM([Balance]) as [Summary]
From
(
SELECT CASE WHEN [Balance] > 0 THEN 'Receieve' ELSE 'Pay' END AS 'Type', [Balance]
From
(
SELECT --Accounts.ID,
-- ( Debit - Credit ) + Previous balance = balance
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0))
+ Accounts.PreviousBalance [Balance]
FROM Accounts
LEFT join Ledger ON Accounts.ID = Ledger.AccountId
WHERE Accounts.Status = 'Active'
GROUP BY Accounts.ID, Accounts.PreviousBalance
) as accountsSummary
) as summary
GROUP BY [Type]
但我知道这不是好的优化方式。这是一些混乱的嵌套子查询方法。必须有更清洁或更好的方法来做到这一点。如何用更好的方法实现它?
试一试:
WITH Balances
AS (
SELECT
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0)) + Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
),
Receipts AS (
SELECT SUM(Balance) Balance
FROM Balances
WHERE Balance > 0
),
Payments AS (
SELECT SUM(Balance) Balance
FROM Balances
WHERE Balance < 0
)
SELECT Balance FROM Receipts
UNION
SELECT Balance FROM Payments
你为什么不使用 LINQ。首先使用 SQL 获取全部数据。然后使用 c#(使用 LINQ)查询它。会容易很多。
在外部查询中,只需区分 positive 值和 group by
中的 negative 值,这就是您所需要的
WITH Balances
AS (
SELECT
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0)) + Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
),
select SUM(Balance)
from Balances
group by case when Balance < 0 then 1 else 0 end
我有一个问题:
Select
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0))
+ Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
它 returns 23 行的所有账户摘要表明有多少客户必须 支付(-ve) 和 接收(+ve) 每个账户:
Balance
=========
800655.00
1869213.50
-6365.25
1148160.00
145743.70
804225.00
157625.00
66440.00
972950.00
780063.50
646680.75
277761.00
347100.00
-70882.50
-7435.50
431940.00
1319340.00
245685.00
372400.00
158220.00
608108.00
6777029.00
1147920.00
现在我想对这个总结做个总结。支付多少和获得多少累计值。例如:
Summary
===========
-84683.25 //sum of all negative values
19077259.45 //sum of all positive values
我是这样做的:
SELECT SUM([Balance]) as [Summary]
From
(
SELECT CASE WHEN [Balance] > 0 THEN 'Receieve' ELSE 'Pay' END AS 'Type', [Balance]
From
(
SELECT --Accounts.ID,
-- ( Debit - Credit ) + Previous balance = balance
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0))
+ Accounts.PreviousBalance [Balance]
FROM Accounts
LEFT join Ledger ON Accounts.ID = Ledger.AccountId
WHERE Accounts.Status = 'Active'
GROUP BY Accounts.ID, Accounts.PreviousBalance
) as accountsSummary
) as summary
GROUP BY [Type]
但我知道这不是好的优化方式。这是一些混乱的嵌套子查询方法。必须有更清洁或更好的方法来做到这一点。如何用更好的方法实现它?
试一试:
WITH Balances
AS (
SELECT
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0)) + Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
),
Receipts AS (
SELECT SUM(Balance) Balance
FROM Balances
WHERE Balance > 0
),
Payments AS (
SELECT SUM(Balance) Balance
FROM Balances
WHERE Balance < 0
)
SELECT Balance FROM Receipts
UNION
SELECT Balance FROM Payments
你为什么不使用 LINQ。首先使用 SQL 获取全部数据。然后使用 c#(使用 LINQ)查询它。会容易很多。
在外部查询中,只需区分 positive 值和 group by
中的 negative 值,这就是您所需要的
WITH Balances
AS (
SELECT
(coalesce(sum(Ledger.Debit), 0) - coalesce(sum(Ledger.Credit), 0)) + Accounts.PreviousBalance [Balance]
FROM
Accounts
LEFT join
Ledger on Accounts.ID = Ledger.AccountId
Where
Accounts.Status = 'Active'
GROUP BY
Accounts.ID, Accounts.PreviousBalance
),
select SUM(Balance)
from Balances
group by case when Balance < 0 then 1 else 0 end