search
HomeDatabaseMysql Tutorial佩特来项目经验小集合(3)___从多表统计金额汇总到一张表

来这个项目主要是佩特来公司各部门及各代理商使用的系统,这个系统其中的一下功能就是统计代理商费用。费用的统计放在了费用池(传统方式统计代理商费用就叫费用池)数据表中,代理商可以根据费用池的中的金额购买东西,费用池中的钱来自于代理商每次填写的

来这个项目主要是佩特来公司各部门及各代理商使用的系统,这个系统其中的一下功能就是统计代理商费用。费用的统计放在了费用池(传统方式统计代理商费用就叫费用池)数据表中,代理商可以根据费用池的中的金额购买东西,费用池中的钱来自于代理商每次填写的维修鉴定单中。

下面看一下这部分的数据表结构:

\

下面的存储过程是汇总材料费及其他费用表的费用并汇总到费用池信息表的存储过程,这段代码有点长,加入了各种判断:

-- =============================================
-- Author:		马兆娟
-- Create date: 2014-7-20 15:32:16
-- Description:	统计材费及其他费用汇总到费用池
-- =============================================
CREATE PROCEDURE [dbo].[PROC_WXJD_CommitFee] 
	-- Add the parameters for the stored procedure here
	@wxjdId int,   --维修鉴定单ID
	@dlsId int     --代理商ID
AS
DECLARE
	@Err1 int,  --声明变量,事务使用
	@Err2 int,   --声明变量,事务使用
	@glf decimal, --管理费
	@gsf decimal, --工时费
	@xj decimal,   --小计
	@totalMoney decimal, --总金额
	@count int,  --整型数据
	@LJGLF decimal, --零件管理费
	@LJGSF decimal, --零件工时费
	@QTFY decimal,  --其他费用
	@WXJDTotalMoney decimal  --总金额

BEGIN
	BEGIN TRANSACTION   --开启事务
	
	SELECT @count=0; --给变量赋值

    --下面统计其他费用
	SELECT @count=count(*) FROM T_DLS_WXJD_Cost_QTFY WHERE FID=@wxjdId
	IF @count>0   --判断其他费用表是否已写入其他费用值,下面给小计赋值
	BEGIN
	   SELECT @xj = CASE SUM(xj) WHEN null THEN 0 ELSE Sum(xj) END  FROM T_DLS_WXJD_Cost_QTFY WHERE FID=@wxjdId;
	END
	ELSE
	BEGIN
	  SELECT @xj=0;
	END
	--下面是统计材料费
	SELECT @count=0;
	SELECT @count=count(*) FROM T_DLS_WXJD_Cost_CLF WHERE Fid =@wxjdId
	IF @count>0  --判断材料费表是否已写入材料费,下面给管理费、工时费赋值
	BEGIN
	   SELECT @glf = CASE SUM(GLF) WHEN null then 0 ELSE SUM(GLF) END ,@gsf = CASE SUM(gsf) WHEN null THEN 0 ELSE Sum(gsf) END 
		  FROM T_DLS_WXJD_Cost_CLF WHERE Fid =@wxjdId
	END
	ELSE
	BEGIN
	 SELECT @glf=0;
	 SELECT @gsf=0;
	END
	--下面给总金额赋值
	SELECT @totalMoney = @xj + @glf + @gsf;
	  --print @totalMoney;
	--下面将从其他费用及材料费中统计的金额写入费用池
	SELECT @count =0;
	SELECT @count=COUNT(*) FROM T_FeeExist WHERE DLSID = @dlsId;
	IF @count>0 --判断费用池表是否已写入某代理商费用,如果已写入过代理商费用,则需向代理商各项费用上添加统计的费用
	BEGIN
	   SELECT @LJGLF=LJGLF,@LJGSF=LJGSF,@QTFY=QTFY,@WXJDTotalMoney=WXJDTotalMoney FROM T_FeeExist WHERE DLSID = @dlsId;
	   SELECT @LJGLF=@LJGLF+@glf;
	   SELECT @LJGSF=@LJGSF+@gsf;
	   SELECT @QTFY=@QTFY+@xj;
	   SELECT @WXJDTotalMoney=@WXJDTotalMoney+@totalMoney;
	   UPDATE T_FeeExist SET LJGLF=@LJGLF,LJGSF=@LJGSF,QTFY=@QTFY,WXJDTotalMoney=@WXJDTotalMoney WHERE DLSID = @dlsId;

	END
	ELSE     --第一次向费用池写入某代理商费用,添加新记录
	BEGIN
	INSERT INTO	T_FeeExist(DLSID,LJGLF,LJGSF,QTFY,WXJDTotalMoney) values(@dlsId,@glf,@gsf,@xj,@totalMoney);  
	END
	SET @err1=@@ERROR 
	
	--更新维修鉴定表是否已提交到费用池字段
	UPDATE T_DLS_WXJD SET IsFYC='是' WHERE ID=@wxjdId
	SET @err2=@@ERROR 
	
	
  	   --判断是否出错
		IF (@Err1=0 and @Err2=0 )
			COMMIT TRANSACTION   --提交事务
		ELSE
			ROLLBACK TRANSACTION  --事务回滚
END

统计费用就简单的介绍到这里了,这里主要记录的关键点就是如何汇总各表的数据及存储过程中对数值进行判断!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools