我有两个问题。两者都返回
select sum (Amount) as 'Less then 30 days', [Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM [Fluids Management$Detailed Cust_ Ledg_ Entry]
group by [Fluids Management$Cust_ Ledger Entry].[Customer No_]
select sum ([Debit Amount]) as 'Less then 30 days', [Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM [Fluids Management$Detailed Cust_ Ledg_ Entry]
group by [Fluids Management$Cust_ Ledger Entry].[Customer No_]
都有更多的深入标准,我只是没有包含它。无论如何,我想采取第一个查询,并检查第二个查询中是否存在客户。如果是这样,请将金额总和加到借方金额中。如果id没有显示在第二个查询中,则只返回第一个查询中的总和。它全部归客户所有。请帮忙
答案 0 :(得分:0)
eaiset方式可能是内联UNION查询的SUM
SELECT SUM (AMOUNT), [Customer No_]
FROM
(
select Amount
[Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM
[Fluids Management$Detailed Cust_ Ledg_ Entry]
UNION ALL
select [Debit Amount] as 'Less then 30 days',
[Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM [Fluids Management$Detailed Cust_ Ledg_ Entry]
)
GROUP BY [Customer No_]
您没有指定数据库,因此您可能需要为内联视图设置别名才能使其正常工作
答案 1 :(得分:0)
将两个查询放在派生表中并LEFT JOIN:
SELECT a.[Customer No_], a.'Less then 30 days' + COALESCE(b.'Less then 30 days', 0)
FROM
(
SELECT SUM (Amount) AS 'Less then 30 days', [Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM [Fluids Management$Detailed Cust_ Ledg_ Entry]
GROUP BY [Fluids Management$Cust_ Ledger Entry].[Customer No_]
) AS a
LEFT JOIN
(
SELECT SUM ([Debit Amount]) AS 'Less then 30 days', [Fluids Management$Cust_ Ledger Entry].[Customer No_]
FROM [Fluids Management$Detailed Cust_ Ledg_ Entry]
GROUP BY [Fluids Management$Cust_ Ledger Entry].[Customer No_]
) AS b
ON a.[Customer No_] = b.[Customer No_]
但是当您访问同一个表两次时,您可以使用SUM(CASE)将其更改为单个查询