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

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

Jun 07, 2016 pm 03:59 PM
Summary experience statistics this gather project

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

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

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

\

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

-- =============================================
-- 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
Copy after login

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

Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Apr 09, 2024 pm 03:20 PM

Fermat's last theorem, about to be conquered by AI? And the most meaningful part of the whole thing is that Fermat’s Last Theorem, which AI is about to solve, is precisely to prove that AI is useless. Once upon a time, mathematics belonged to the realm of pure human intelligence; now, this territory is being deciphered and trampled by advanced algorithms. Image Fermat's Last Theorem is a "notorious" puzzle that has puzzled mathematicians for centuries. It was proven in 1993, and now mathematicians have a big plan: to recreate the proof using computers. They hope that any logical errors in this version of the proof can be checked by a computer. Project address: https://github.com/riccardobrasca/flt

Share an easy way to package PyCharm projects Share an easy way to package PyCharm projects Dec 30, 2023 am 09:34 AM

Share the simple and easy-to-understand PyCharm project packaging method. With the popularity of Python, more and more developers use PyCharm as the main tool for Python development. PyCharm is a powerful integrated development environment that provides many convenient functions to help us improve development efficiency. One of the important functions is project packaging. This article will introduce how to package projects in PyCharm in a simple and easy-to-understand way, and provide specific code examples. Why package projects? Developed in Python

PyCharm Practical Tips: Convert Project to Executable EXE File PyCharm Practical Tips: Convert Project to Executable EXE File Feb 23, 2024 am 09:33 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of development tools and environment configurations, allowing developers to write and debug code more efficiently. In the process of using PyCharm for Python project development, sometimes we need to package the project into an executable EXE file to run on a computer that does not have a Python environment installed. This article will introduce how to use PyCharm to convert a project into an executable EXE file, and give specific code examples. head

A closer look at PyCharm: a quick way to delete projects A closer look at PyCharm: a quick way to delete projects Feb 26, 2024 pm 04:21 PM

Title: Learn more about PyCharm: An efficient way to delete projects. In recent years, Python, as a powerful and flexible programming language, has been favored by more and more developers. In the development of Python projects, it is crucial to choose an efficient integrated development environment. As a powerful integrated development environment, PyCharm provides Python developers with many convenient functions and tools, including deleting project directories quickly and efficiently. The following will focus on how to use delete in PyCharm

Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ​​such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

How to use SQL statements for data aggregation and statistics in MySQL? How to use SQL statements for data aggregation and statistics in MySQL? Dec 17, 2023 am 08:41 AM

How to use SQL statements for data aggregation and statistics in MySQL? Data aggregation and statistics are very important steps when performing data analysis and statistics. As a powerful relational database management system, MySQL provides a wealth of aggregation and statistical functions, which can easily perform data aggregation and statistical operations. This article will introduce the method of using SQL statements to perform data aggregation and statistics in MySQL, and provide specific code examples. 1. Use the COUNT function for counting. The COUNT function is the most commonly used

Do you know how to count people in excel? Do you know how to count people in excel? Mar 20, 2024 pm 01:30 PM

As one of the commonly used office software, Excel is an indispensable good partner in our life and work. We often use it to count people, which is also the most common operation. For veterans who are familiar with Excel, using it to count people is a matter of minutes; but if you are a novice, using ecxel to count people is simply difficult! So how to use it? I compiled a document today, I hope it can help you! Let’s take a look together! Method 1: [Use functions to count people] (as shown in the picture) We enter [=COUNT(B2:B6)] in the bottom unit cell; then, press [Enter] to get the number of people. Method 2: [Use the status bar to count people] (as shown in the picture). 1. We click the mouse

Basic tutorial: Create a Maven project using IDEA Basic tutorial: Create a Maven project using IDEA Feb 19, 2024 pm 04:43 PM

IDEA (IntelliJIDEA) is a powerful integrated development environment that can help developers develop various Java applications quickly and efficiently. In Java project development, using Maven as a project management tool can help us better manage dependent libraries, build projects, etc. This article will detail the basic steps on how to create a Maven project in IDEA, while providing specific code examples. Step 1: Open IDEA and create a new project Open IntelliJIDEA

See all articles