如何从 sql 中的两个表中获得优秀
How to get outstanding from two tables in sql
我有两个 Table:
- 卖出
- 列表项
收据
我已经写了查询,但是添加了一些数量,因为它在第二个 table
中找到
1) 卖出 Table:
Create Table Sells(
ID int,
Invone varchar (30),
InvDate DateTime ,
Year varchar (15),
Type varchar (10) ,
Terms varchar (20) ,
Days int ,
DueDate DateTime,
Pieces int ,
Carats Real ,
Price Real ,
GrossAmt Real ,
Discount1 Real ,
DisAmt1 Real ,
Discount2 Real,
DisAmt2 Real ,
FinalAmt Real,
Party varchar (100),
Party_ID int,
Person varchar (100),
Shape varchar (30),
Quality varchar (30),
StockId int,
SalesExe varchar (50),
Remarks varchar (200),
CreatedDate DateTime,
ModifiedDate DateTime,
Username varchar (50))
2) 收据Table:
Create Table Receipt (
ID int ,
Invone varchar (30),
RDate DateTime,
Year varchar (15),
Type varchar (10),
HKD Real,
Rate Real,
ShortAmt Real,
RecievedAmt Real,
TotalRecievedAmt Real,
FinalAmt Real,
Party varchar (100),
Party_ID int,
Remarks varchar (200),
CreatedDate DateTime,
ModifiedDate DateTime,
Username varchar (50)
)
这是我的查询:
select s.Invone,s.InvDate,s.Type,s.year,s.Terms,s.DueDate,s.Party,s.Party_ID,sum(s.Pieces) AS TotalPieces,
round(sum(s.Carats), 3) AS TotalCarats, round(sum(s.GrossAmt), 2) AS TotalGrossAmt, round(sum(s.DisAmt1), 2) AS TotalDis1,
round(sum(s.DisAmt2), 2) AS TotalDis2, round(sum(s.FinalAmt), 2) AS TotalFinalAmt, isnull(round(sum(HKD), 2), 0) AS TotalHKD,
isnull(round(rate, 2), 0) AS Rate, isnull(round(sum(ShortAmt), 2), 0) AS TotalShort,isnull(round(sum(RecievedAmt), 2), 0) AS RecievedAmt,
isnull(round(sum(TotalRecievedAmt), 2), 0) AS TotalRecievedAmt, round(sum(s.FinalAmt) - isnull(sum(TotalRecievedAmt), 0), 2) AS Balance
from Sells s
left join Recipt r on s.Invone = r.Invone WHERE s.Party = 'Buyer'
group by s.Invone,s.InvDate,s.Party,s.Party_ID,s.Type,s.Year,s.Terms,s.DueDate,rate
having (sum(s.FinalAmt) - sum(isnull(TotalRecievedAmt, 0))) 0
order by s.InvDate, s.Invone
这是我得到的输出
1:Image of Output
我需要像 Invone 这样的输出应该只出现一次。和 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 不应添加 2 次或 3 次。
如果我有 Invone 没有。 15 两次出售 table 和 15 号的三个收据。在部分付款。那么在输出 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 中不应添加 3 次。它应该仅显示来自销售 table 的 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 的总和。
从收据 Table 它应该显示 TotalHKD、TotalShort、RecievedAmt、TotalRecievedAmt
的总和
我得到了解决方案
select S.Invone,S.InvDate,S.Type,S.Year,S.Terms,S.DueDate,S.Party,S.Party_ID,S.TotalPieces,S.TotalCarats,S.TotalGrossAmt,S.TotalDis1,S.TotalDis2,S.TotalFinalAmt,isnull(R.TotalHKD,0) AS TotalHKD,isnull(R.Rate,0) AS Rate,isnull(R.TotalShort,0) AS TotalShort,isnull(R.RecievedAmt,0) AS RecievedAmt,isnull(R.TotalRecievedAmt,0) AS TotalRecievedAmt,(round(S.TotalFinalAmt - isnull(R.TotalRecievedAmt, 0), 2)) AS Balance from
(select Invone, isnull(round(sum(HKD), 2), 0) AS TotalHKD,isnull(round(rate, 2), 0) AS Rate, isnull(round(sum(ShortAmt), 2), 0) AS TotalShort,isnull(round(sum(RecievedAmt), 2), 0) AS RecievedAmt,
isnull(round(sum(TotalRecievedAmt), 2), 0) AS TotalRecievedAmt from Recipt
group by Invone,Rate)as R
right outer join
(select Invone,InvDate,Type,Year,Terms,DueDate,Party,Party_ID,sum(Pieces)as TotalPieces, round(sum(Carats), 3) AS TotalCarats, round(sum(GrossAmt), 2) AS TotalGrossAmt, round(sum(DisAmt1), 2) AS TotalDis1,
round(sum(DisAmt2), 2) AS TotalDis2, round(sum(FinalAmt), 2) AS TotalFinalAmt from Sells
group by Invone,InvDate,Type,Year,Terms,DueDate,Party,Party_ID )as S
on S.Invone=R.Invone
WHERE S.TotalFinalAmt-isnull(R.TotalRecievedAmt,0) 0
我有两个 Table:
- 卖出
- 列表项
收据
我已经写了查询,但是添加了一些数量,因为它在第二个 table
中找到1) 卖出 Table:
Create Table Sells(
ID int,
Invone varchar (30),
InvDate DateTime ,
Year varchar (15),
Type varchar (10) ,
Terms varchar (20) ,
Days int ,
DueDate DateTime,
Pieces int ,
Carats Real ,
Price Real ,
GrossAmt Real ,
Discount1 Real ,
DisAmt1 Real ,
Discount2 Real,
DisAmt2 Real ,
FinalAmt Real,
Party varchar (100),
Party_ID int,
Person varchar (100),
Shape varchar (30),
Quality varchar (30),
StockId int,
SalesExe varchar (50),
Remarks varchar (200),
CreatedDate DateTime,
ModifiedDate DateTime,
Username varchar (50))
2) 收据Table:
Create Table Receipt (
ID int ,
Invone varchar (30),
RDate DateTime,
Year varchar (15),
Type varchar (10),
HKD Real,
Rate Real,
ShortAmt Real,
RecievedAmt Real,
TotalRecievedAmt Real,
FinalAmt Real,
Party varchar (100),
Party_ID int,
Remarks varchar (200),
CreatedDate DateTime,
ModifiedDate DateTime,
Username varchar (50)
)
这是我的查询:
select s.Invone,s.InvDate,s.Type,s.year,s.Terms,s.DueDate,s.Party,s.Party_ID,sum(s.Pieces) AS TotalPieces, round(sum(s.Carats), 3) AS TotalCarats, round(sum(s.GrossAmt), 2) AS TotalGrossAmt, round(sum(s.DisAmt1), 2) AS TotalDis1, round(sum(s.DisAmt2), 2) AS TotalDis2, round(sum(s.FinalAmt), 2) AS TotalFinalAmt, isnull(round(sum(HKD), 2), 0) AS TotalHKD, isnull(round(rate, 2), 0) AS Rate, isnull(round(sum(ShortAmt), 2), 0) AS TotalShort,isnull(round(sum(RecievedAmt), 2), 0) AS RecievedAmt, isnull(round(sum(TotalRecievedAmt), 2), 0) AS TotalRecievedAmt, round(sum(s.FinalAmt) - isnull(sum(TotalRecievedAmt), 0), 2) AS Balance from Sells s left join Recipt r on s.Invone = r.Invone WHERE s.Party = 'Buyer' group by s.Invone,s.InvDate,s.Party,s.Party_ID,s.Type,s.Year,s.Terms,s.DueDate,rate having (sum(s.FinalAmt) - sum(isnull(TotalRecievedAmt, 0))) 0 order by s.InvDate, s.Invone
这是我得到的输出 1:Image of Output
我需要像 Invone 这样的输出应该只出现一次。和 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 不应添加 2 次或 3 次。
如果我有 Invone 没有。 15 两次出售 table 和 15 号的三个收据。在部分付款。那么在输出 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 中不应添加 3 次。它应该仅显示来自销售 table 的 TotalPeices、TotalCarats、TotalGrossAmt、TotalDis1、TotalDis2、TotalFinalAmt 的总和。 从收据 Table 它应该显示 TotalHKD、TotalShort、RecievedAmt、TotalRecievedAmt
的总和我得到了解决方案
select S.Invone,S.InvDate,S.Type,S.Year,S.Terms,S.DueDate,S.Party,S.Party_ID,S.TotalPieces,S.TotalCarats,S.TotalGrossAmt,S.TotalDis1,S.TotalDis2,S.TotalFinalAmt,isnull(R.TotalHKD,0) AS TotalHKD,isnull(R.Rate,0) AS Rate,isnull(R.TotalShort,0) AS TotalShort,isnull(R.RecievedAmt,0) AS RecievedAmt,isnull(R.TotalRecievedAmt,0) AS TotalRecievedAmt,(round(S.TotalFinalAmt - isnull(R.TotalRecievedAmt, 0), 2)) AS Balance from (select Invone, isnull(round(sum(HKD), 2), 0) AS TotalHKD,isnull(round(rate, 2), 0) AS Rate, isnull(round(sum(ShortAmt), 2), 0) AS TotalShort,isnull(round(sum(RecievedAmt), 2), 0) AS RecievedAmt, isnull(round(sum(TotalRecievedAmt), 2), 0) AS TotalRecievedAmt from Recipt group by Invone,Rate)as R right outer join (select Invone,InvDate,Type,Year,Terms,DueDate,Party,Party_ID,sum(Pieces)as TotalPieces, round(sum(Carats), 3) AS TotalCarats, round(sum(GrossAmt), 2) AS TotalGrossAmt, round(sum(DisAmt1), 2) AS TotalDis1, round(sum(DisAmt2), 2) AS TotalDis2, round(sum(FinalAmt), 2) AS TotalFinalAmt from Sells group by Invone,InvDate,Type,Year,Terms,DueDate,Party,Party_ID )as S on S.Invone=R.Invone WHERE S.TotalFinalAmt-isnull(R.TotalRecievedAmt,0) 0