我正在尝试使用联接通过多个表获取特定字段的总和。尝试将所有值汇总在一起时,我得到的SUM错误。
我的表State为
| STATE | MONTH | ID | TOTAL |
--------------------------------
| FL | July | 1 | 10000 |
| CA | April | 32 | 2000 |
我还有另一个表Balance
| STATE | Balance|
-------------------
| FL | 100|
| FL | 200|
| CA | 300|
| CA | 200|
| CA | 100|
我还有一笔表贷款
| STATE | Loan|
-------------------
| FL | 500|
| FL | 600|
| CA | 700|
| CA | 100|
| CA | 200|
查询后我想要的是
| STATE | Loan| Balance|
----------------------------
| FL | 1100| 300|
| CA | 1000| 600|
当我尝试使用以下查询时,我正确获取了状态为贷款的款项,
SELECT
S.STATE,
SUM(L.Loan)
FROM State AS S
INNER JOIN Loan AS L ON L.STATE = S.STATE
GROUP BY
S.STATE
我得到以下结果,
| STATE | Loan|
--------------------
| FL | 1100|
| CA | 1000|
类似地,我可以从余额表中获得总和。 我想要实现的是使用单个事务从两个表中获得总和。
尽管如此,我能够获得所需的值,
SELECT
STATE AS STATE
SUM(DataSetB.Balance) AS Balance
FROM
(
SELECT
STATE AS STATE,
B.Balance AS Balance
FROM
(
SELECT
S.STATE AS STATE,
SUM(L.Loan) AS Loan,
FROM State AS S
INNER JOIN Loan AS L ON L.STATE = S.STATE
GROUP BY S.STATE
)
AS DataSetL
INNER JOIN Balance AS B ON B.STATE = DataSetL.STATE
GROUP BY
DataSetL.STATE, B.Balance
) AS DataSetB
GROUP BY
DataSetB.STATE
但是,在我拥有的大数据集上,这不太可行。 我尝试过,
SELECT
S.STATE AS STATE,
SUM(L.Loan) AS Loan,
SUM(B.Balance) AS Balance
FROM State AS S
INNER JOIN Loan AS L ON L.STATE = S.STATE
INNER JOIN Balance AS B ON B.STATE = S.STATE
GROUP BY
S.STATE
但这给我的价值是实际价值的倍数。实际上,它是子表中存在的行数的倍数。
我并没有要求确切的解决方案,但是任何一种指针都可以。
答案 0 :(得分:4)
您可以尝试一下,使用UNION ALL
合并Balance
和Loan
表并使用一些技巧
Balance
填充Loan
列集0 Loan
填充Balance
列集0 然后将JOIN
与State
一起做SUM
create table State(
STATE varchar(50)
);
insert into State values ('FL');
insert into State values ('CA');
create table Balance(
STATE varchar(50),
Balance int
);
insert into Balance values ('FL',100);
insert into Balance values ('FL',200);
insert into Balance values ('CA',300);
insert into Balance values ('CA',200);
insert into Balance values ('CA',100);
create table Loan(
STATE varchar(50),
loan int
);
insert into loan values ( 'FL',500);
insert into loan values ( 'FL',600);
insert into loan values ( 'CA',700);
insert into loan values ( 'CA',100);
insert into loan values ( 'CA',200);
查询1 :
SELECT s.STATE,SUM(t1.Loan) as 'Loan',SUM(t1.Balance) as 'Balance'
FROM
(
SELECT STATE,0 AS Loan,Balance
FROM Balance
UNION ALL
SELECT STATE,Loan,0
FROM Loan
) t1
INNER JOIN State s on s.STATE = t1.STATE
GROUP BY s.STATE
Results :
| STATE | Loan | Balance |
|-------|------|---------|
| CA | 1000 | 600 |
| FL | 1100 | 300 |
答案 1 :(得分:3)
在单独的子查询中对每个表进行汇总,然后加入:
SELECT
S.STATE AS STATE,
L.Loan,
B.Balance
FROM State AS S
LEFT JOIN (select state, sum(loan) as Loan
from loan
group by state) AS L ON L.STATE = S.STATE
LEFT JOIN (select state, sum(Balance) as balance
from balance
group by state) AS B ON B.STATE = S.STATE
(如果一个州没有贷款或余额行,则可以左加入)。
答案 2 :(得分:0)
尝试一下:
SELECT S.STATE, L.Loan as TotalLoan, B.Balance as TotalBalance
FROM State AS S
LEFT JOIN (select STATE, sum(Loan) as Loan
from Loan
group by STATE) AS L
ON L.STATE = S.STATE
LEFT JOIN (select STATE, sum(Balance) as Balance
from Balance
group by STATE) AS B
ON B.STATE = S.STATE
结果是:
| STATE |TotalLoan|TotalBalance|
----------------------------
| FL | 1100 | 300 |
| CA | 1000 | 600 |
答案 3 :(得分:0)
您能尝试一下吗?
Select S.State, B.Balance,L.Loan from State S
inner join (select state, sum(loan) as Loan from Loan group by state) L
inner join (select state, sum(balance) as Balance from Balance group by state) B
on L.state=S.state and B.State=S.State;
答案 4 :(得分:0)
使用子查询:首先连接借贷表和余额表,然后将状态表与该派生表结果连接起来
SELECT
S.STATE AS STATE, Loan,Balance
FROM State AS S inner join
(select L.state,sum(loan) as loan, sum(balance) as balance
from Loan AS L INNER JOIN Balance AS B ON B.STATE = S.STATE
GROUP BY L.STATE)X on s.state=x.state
答案 5 :(得分:0)
我建议使用通用表表达式,因为它很“干净”。
尝试以下操作:
WITH CTE_TotalLoan AS (
SELECT
State,
SUM(Loan) AS TotalLoan
FROM Loan
GROUP BY State
),
CTE_TotalBalance AS (
SELECT
State,
SUM(Balance) AS TotalBalance
FROM Balance
GROUP BY State
)
SELECT
S.State,
TotalLoan,
TotalBalance
FROM State AS S
INNER JOIN CTE_TotalLoan AS L ON L.STATE = S.STATE
INNER JOIN CTE_TotalBalance AS B ON B.STATE = S.STATE
答案 6 :(得分:0)
尝试一下:
Create Table #Loan([State] Varchar(10),Loan Bigint)
Create Table #Balance([State] Varchar(10),Balance Bigint)
Insert Into #Loan
Select 'FL',500 Union All
Select 'FL',600 Union All
Select 'CA',700 Union All
Select 'CA',100 Union All
Select 'CA',200
Insert Into #Balance
Select 'FL',100 Union All
Select 'FL',200 Union All
Select 'CA',300 Union All
Select 'CA',200 Union All
Select 'CA',100
;with cteLoan
AS(
Select ROW_NUMBER() Over(Partition by State order by State) As Rn, * from #Loan
)
,
cteBalance
AS(
Select ROW_NUMBER() Over(Partition by State order by State) As Rn, * from #Balance
)
Select DISTINCT ISNULL(l.State,b.State) as State
,SUM(ISNULL(Loan,0)) As Loan
,SUM(ISNULL(Balance,0)) As Balance
from cteLoan l
FULL OUTER JOIN cteBalance b
on l.State=b.State and l.rn=b.rn
Group by ISNULL(l.State,b.State)
Drop Table #Loan
Drop Table #Balance
答案 7 :(得分:0)
您也可以使用APPLY
:
SELECT s.*, l.loan, b.balance
FROM State s OUTER APPLY
(SELECT SUM(l.loan) loan
FROM loan l
WHERE l.state = s.state
) l OUTER APPLY
(SELECT SUM(b.balance) balance
FROM balance b
WHERE b.state = s.state
) b;