谁能帮我出从三个表计算总会费一个CREATE PROCEDURE语句? 这里有与他们一起的数据表..
表格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
表3
accountno Date ReceivedAmount MoneyReceiptNo
001 1/1/2012 6500 G 256412
002 1/2/2012 5200 D 246521
在这里,我要提及的是,总的会费会被计算
(currentamount + anotheramount) - receivedamount
我试图通过以下存储过程来做到这一点。
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
但是,这并不工作。 请有人帮助我。
你的样品为我工作。 我唯一改变的是“日期”,以transdate。 我强烈建议避免“日期”作为列名。 我也改变了走样了一点,但本来应该还好吧。 我thinkg @Gordon Linoff是正确的 - 你可以有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
下面是我的结果:
Executivename TotalDues
Soma 12600.00
我能想到的两个问题。 首先是该账号在任一表1或表2这将增加额外的行复制。
第二个是,有在表三行不在表2。这意味着总和内添加是NULL,因为值中的一个是NULL。 您可以按以下方法之一来解决此:
sum(table_2.currentamount) + sum(table_2.anotheramount) - sum(table_3.receivedamount)
要么
sum(coalesce(table_2.currentamount, 0.0) + coalesce(table_2.anotheramount, 0.0) - coalesce(table_3.receivedamount, 0.0) ) as TotalDues
我认为这将是一个UNION查询,下面的例子更直截了当:
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