问题
Can anyone help me by showing a create procedure statement for calculating Total Dues from three tables? Here are the tables along with their data ..
Table_1
accountno shipername shiperaddress Executivename
001 john 123, London Paul
002 Robi 127, China Soma
Table_2
Executivename shipername shiperaddress accountno currentamount anotheramount
paul john 123,london 001 10500 12000
soma robi 127,china 002 11000 6800
Table_3
accountno Date ReceivedAmount MoneyReceiptNo
001 1/1/2012 6500 G 256412
002 1/2/2012 5200 D 246521
Here I am to mention that the Total Dues will be calculated as
(currentamount + anotheramount) - receivedamount
I tried to do that by the following stored procedure.
CREATE PROCEDURE [dbo].[rptexetotaldues] @Executivename varchar(20)
AS BEGIN
select
table_1.Executivename,
sum(table_2.currentamount + table_2.anotheramount
- table_3.receivedamount ) as TotalDues
from
table_1
full join
table_2 on table_1.accountno = table_2.accountno
join
table_3 on table_3.accountno = table_1.accountno
where
table_1.Executivename = @Executivename
group by
table_1.Executivename
end
But that doesn't work. Please somebody help me.
回答1:
Your sample worked for me. The only thing I changed is "Date" to transdate. I strongly recommend avoiding "Date" as a column name. I also changed the aliasing a bit, but that should have been allright. I thinkg @Gordon Linoff is right - you could have an issue with NULLS.
DECLARE @table_1 TABLE (accountno char(5), shipername char(20), shiperaddress char(40), Executivename varchar(20))
INSERT INTO @table_1 VALUES ('001', 'john', '123, London', 'Paul')
INSERT INTO @table_1 VALUES ('002','Robi','127, China','Soma')
DECLARE @table_2 TABLE (Executivename varchar(20), shipername char(20), shiperaddress char(40),
accountno char(20), currentamount decimal(10,2), anotheramount decimal(10,2))
INSERT INTO @table_2 VALUES ('paul', 'john','123,london','001',10500, 12000)
INSERT INTO @table_2 VALUES ('soma', 'robi', '127,china', '002', 11000, 6800)
DECLARE @table_3 TABLE(accountno char(20), tranDate datetime, ReceivedAmount decimal(10,2), MoneyReceiptNo char(10))
INSERT INTO @table_3 VALUES ('001', '1/1/2012', 6500, 'G 256412')
INSERT INTO @table_3 VALUES ('002', '1/2/2012', 5200,'D 246521')
DECLARE @Executivename varchar(20)
--SET @Executivename = 'Paul'
SET @Executivename = 'Soma'
select
tb1.Executivename,
sum(tb2.currentamount + tb2.anotheramount - tb3.receivedamount ) as TotalDues
from
@table_1 tb1
full join @table_2 tb2 on tb1.accountno = tb2.accountno
join @table_3 tb3 on tb3.accountno = tb1.accountno
where
tb1.Executivename=@Executivename group by tb1.Executivename
Here are my results:
Executivename TotalDues
Soma 12600.00
回答2:
I can think of two problems. First is that the account number is duplicated in either table 1 or table 2. This will add extra rows.
The second is that there are rows in table three that are not in table 2. This means that the addition within the sum is NULL because one of the values is NULL. You can fix this in one of these ways:
sum(table_2.currentamount) + sum(table_2.anotheramount) - sum(table_3.receivedamount)
or
sum(coalesce(table_2.currentamount, 0.0) + coalesce(table_2.anotheramount, 0.0) - coalesce(table_3.receivedamount, 0.0) ) as TotalDues
回答3:
I think it would be more straightforward as a UNION query, example below:
CREATE PROCEDURE [dbo].[rptexetotaldues] @Executivename varchar(20)
AS BEGIN
SELECT SUB.ACCOUNTNO, SUM(SUB.DUE) AS TOTALDUE FROM
(SELECT ACCOUNTNO
, CURRENTAMOUNT AS DUE
FROM TABLE_2
INNER JOIN TABLE_1 -- WILL ONLY WORK IF ACCOUNTNO IS UNIQUE WITHIN TABLE_1
ON TABLE_1.ACCOUNTNO = TABLE2.ACCOUNTNO
WHERE TABLE_1.EXECUTIVENAME = @Executivename
UNION ALL
SELECT ACCOUNTNO
, ANOTHERAMOUNT AS DUE
FROM TABLE_2
INNER JOIN TABLE_1
ON TABLE_1.ACCOUNTNO = TABLE2.ACCOUNTNO
WHERE TABLE_1.EXECUTIVENAME = @Executivename
UNION ALL
SELECT ACCOUNTNO
, -RECEIVEDAMOUNT AS DUE -- NOTE NEGATIVE SIGN
FROM TABLE 3
INNER JOIN TABLE_1
ON TABLE_1.ACCOUNTNO = TABLE3.ACCOUNTNO
WHERE TABLE_1.EXECUTIVENAME = @Executivename
) SUB
GROUP BY SUB.ACC
来源:https://stackoverflow.com/questions/10558484/how-to-create-sql-procedure-for-calculating-total-dues