任何人都可以通过显示用于计算三个表总计费用的创建过程语句来帮助我吗?以下是表格及其数据..
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
我要提到的是,总费用将计算为
(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
但这不起作用。请有人帮帮我
答案 0 :(得分:2)
你的样本为我工作。我唯一改变的是“日期”转换。我强烈建议避免使用“日期”作为列名。我也改变了一些别名,但那应该是好的。我认为@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 :(得分:0)
我可以想到两个问题。首先是帐号在表1或表2中重复。这将添加额外的行。
第二个是表3中有些行不在表2中。这意味着sum中的加法是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
答案 2 :(得分:0)
我认为它作为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