2

任何人都可以通过显示用于从三个表计算总会费的创建过程语句来帮助我吗?这是表格及其数据..

表格1

accountno   shipername   shiperaddress   Executivename
   001      john         123, London          Paul
   002      Robi         127, China           Soma

表_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

但这不起作用。请有人帮助我。

4

3 回答 3

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
于 2012-05-11T21:25:11.317 回答
0

我可以想到两个问题。首先是帐号在表 1 或表 2 中重复。这将添加额外的行。

第二个是表 3 中有一些行不在表 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 
于 2012-05-11T21:15:52.097 回答
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
于 2012-05-11T21:26:04.937 回答