sql - 如何创建 SQL 过程来计算总会费

标签 sql visual-studio sql-server-2000 crystal-reports-8.5

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

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

但这行不通。请有人帮助我。

最佳答案

你的样本对我有用。我唯一改变的是要转换的“日期”。我强烈建议避免使用“日期”作为列名称。我还稍微改变了别名,但这应该没问题。我认为 @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

关于sql - 如何创建 SQL 过程来计算总会费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558484/

相关文章:

sql - Oracle:使用正则表达式从查询中排除结果

java - ORA-00942: 表或 View 不存在

c# - resx 资源文件显示警告 "The resource name is not a valid identifier"

c++ - 使用 CMake 安装的 PDB 文件

sql-server-2005 - 用于 SQL Server 2000 的 SQL Server Management Studio

以逗号分隔列表形式返回 SQL 数据

sql-server - SQL Server : How to abort a series of batches in Query Analyzer?

sql - 在 MySQL 中使用集合的更快方法

sql同名但值不同

c# - 如何在 C#/VS 社区中将程序导出到设计器之外