SQL server 自定义工资公式设计
目的:
工资系统中,在计算各工资项目的时候,用户能自定义公式。这里的讲述是从数据库方设计方面考虑,做简要的数据模拟和实体、存储过程的设计。
收集数据:
1.Employee员工信息数据(EmployeeID,Name,Sex,Department,Position,Joindate(到职日期),DimissionDate(离职日期)… …)
2.Salay员工工资清单(SalaryMonth(工资月份),Employee(员工),基本工资,岗位津贴,技术津贴,住宿费,上月余额,应得工资,本月余额,实得工资 … …)
分析:
这里只是作为Demo,就制作一个简单的数据分析。
1.Employee员工信息就认为一个对象实体,不做详细的数据提取筛分。
2.在Salay员工工资清单,可以把“基本工资,岗位津贴,技术津贴,住宿费 。。。”这些项提取出来,归类为SalaryItem工资项目;SalaryItem工资项目还可以再分类为“公式项”与“非公式项”。(注:“公式项”是指可以使用公式来计算非手工输入的工资项目)当然还可以根据实际的需要分类的更详细,清晰。
数据表:
根据上面的简单分析,可以设计出作Demo使用的表,
1.Emplyee:员工信息表。
2.Salary:员工工资表
3.SalaryItem:工资项目表
4.Formulary:公式表
5.SysSalaryItemTypeMTR:工资项目分类表
建表:
USE [test]GO/*建表脚本*/If object_id('Salary') Is Not Null Drop Table SalaryIf object_id('Formulary') Is Not Null Drop Table FormularyIf object_id('Employee') Is Not Null Drop Table EmployeeIf object_id('SalaryItem') Is Not Null Drop Table SalaryItemIf object_id('SysSalaryItemTypeMTR') Is Not Null Drop Table SysSalaryItemTypeMTRGoCREATE TABLE [SysSalaryItemTypeMTR]( [ID] [smallint] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,CONSTRAINT [PK_SysSalaryItemTypeMTR] PRIMARY KEY CLUSTERED( [ID] ASC))CREATE TABLE [Employee]( [ID] [int] IDENTITY(1,1) NOT NULL, [EmployeeNo] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [Sex] [nchar](1) COLLATE Chinese_PRC_CI_AS NOT NULL, [Department] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [Position] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [JoinDate] [datetime] NULL, [DimissionDate] [datetime] NULL,CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED( [ID] ASC))CREATE TABLE [SalaryItem]( [ID] [smallint] IDENTITY(1,1) NOT NULL, [ItemType] [smallint] NOT NULL, [Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,CONSTRAINT [PK_SalaryItem] PRIMARY KEY CLUSTERED( [ID] ASC))ALTER TABLE [SalaryItem] WITH CHECK ADD CONSTRAINT [FK_SalaryItem_SysSalaryItemTypeMTR] FOREIGN KEY([ItemType])REFERENCES [SysSalaryItemTypeMTR] ([ID])CREATE TABLE [Salary]( [ID] [int] Identity(1,1)NOT NULL, [EmployeeID] [int] NOT NULL, [SalaryMonth] [datetime] NULL, [SalaryItemID] [smallint] NOT NULL, [Amount] [money] NULL,CONSTRAINT [PK_Salary] PRIMARY KEY CLUSTERED( [ID] ASC))ALTER TABLE [Salary] WITH CHECK ADD CONSTRAINT [FK_Salary_Employee] FOREIGN KEY([EmployeeID])REFERENCES [Employee] ([ID])ALTER TABLE [Salary] WITH CHECK ADD CONSTRAINT [FK_Salary_SalaryItem] FOREIGN KEY([SalaryItemID])REFERENCES [SalaryItem] ([ID])Create Table Formulary( ID int Identity(1,1) Not Null, SalaryItemID Smallint Not Null, Definition nvarchar(4000) Null, Sequence smallint Null, EffectiveDate datetime Null, ExpiryDate datetime Null, Constraint PK_Formulary Primary Key(ID Asc), Constraint FK_Formulary_SalaryItem Foreign Key (SalaryItemID) References SalaryItem(ID))CREATE NONCLUSTERED INDEX [IX_Employee_EmployeeNo] ON [Employee]( [EmployeeNo] ASC)CREATE NONCLUSTERED INDEX [IX_Formulary_SalaryItemID] ON [Formulary]( [SalaryItemID] ASC)CREATE NONCLUSTERED INDEX [IX_Salary_EmployeeID] ON [Salary]( [EmployeeID] ASC)CREATE NONCLUSTERED INDEX [IX_Salary_SalaryMonth_EmployeeID] ON [Salary]( [SalaryMonth] ASC, [EmployeeID] ASC)插入测试数据:Use testGoIf Not Exists(Select 1 From SysSalaryItemTypeMTR) Insert Into SysSalaryItemTypeMTR(Name) Select N'上月项' Union All Select N'固定项' Union All Select N'输入项' Union All Select N'公式项' Union All Select N'文本项'If Not Exists(Select 1 From SalaryItem) Insert Into SalaryItem(ItemType,Name) Select 2,N'基本工资' Union All Select 3,N'其他扣款' Union All Select 3,N'岗位津贴' Union All Select 3,N'技术津贴' Union All Select 3,N'住宿费' Union All Select 1,N'上月余额' Union All Select 4,N'应得工资' Union All Select 4,N'本月余额' Union All Select 4,N'实得工资'If Not Exists(Select 1 From Formulary) Insert Into Formulary(SalaryItemID,Definition,Sequence,EffectiveDate,ExpiryDate) Select 1,N'Isnull([1],0)',1,'20090101','21001231' Union ALl Select 2,N'Isnull([2],0)',2,'20090101','21001231' Union ALl Select 3,N'Isnull([3],0)',3,'20090101','21001231' Union ALl Select 4,N'Isnull([4],0)',4,'20090101','21001231' Union ALl Select 5,N'Isnull([5],0)',5,'20090101','21001231' Union ALl Select 6,N'Isnull([8],0)',6,'20090101','21001231' Union ALl Select 7,N'Isnull([1],0)+Isnull([2],0)+Isnull([3],0)+Isnull([4],0)+Isnull([5],0)+Isnull([6],0)',7,'20090101','21001231' Union ALl Select 8,N'Case Convert(char(6),DimissionDate,112)+''01'' When SalaryMonth Then 0 Else Isnull([7],0)%10 End ',8,'20090101','21001231' Union ALl Select 9,N'Case Convert(char(6),DimissionDate,112)+''01'' When SalaryMonth Then Isnull([7],0) Else Isnull([7],0)-Isnull([8],0) End',9,'20090101','21001231'If Not Exists(Select 1 From Employee) Insert Into Employee(EmployeeNo,Name,Sex,Department,Position,JoinDate,DimissionDate) Select N'N0001',N'A1',N'男',N'Dep1',N'Pos1','20080101',Null Union All Select N'N0002',N'A2',N'女',N'Dep2',N'Pos2','20080101','20090514' Union All Select N'N0003',N'A3',N'男',N'Dep3',N'Pos3','20080101',NullIf Not Exists(Select 1 From Salary) Insert Into Salary(EmployeeID,SalaryMonth,SalaryItemID,Amount) Select 1,'20090401',1,1000 Union All Select 2,'20090401',1,2000 Union All Select 3,'20090401',1,3000 Union All Select 1,'20090401',2,-10 Union All Select 2,'20090401',2,-20 Union All Select 3,'20090401',2,-30 Union All Select 1,'20090401',3,0 Union All Select 2,'20090401',3,0 Union All Select 3,'20090401',3,0 Union All Select 1,'20090401',4,105 Union All Select 2,'20090401',4,0 Union All Select 3,'20090401',4,107 Union All Select 1,'20090401',5,-60 Union All Select 2,'20090401',5,-60 Union All Select 3,'20090401',5,-60 Union All Select 1,'20090401',6,0 Union All Select 2,'20090401',6,0 Union All Select 3,'20090401',6,0 Union All Select 1,'20090401',7,0 Union All Select 2,'20090401',7,0 Union All Select 3,'20090401',7,0 Union All Select 1,'20090401',8,0 Union All Select 2,'20090401',8,0 Union All Select 3,'20090401',8,0 Union All Select 1,'20090401',9,0 Union All Select 2,'20090401',9,0 Union All Select 3,'20090401',9,0 Union All Select 1,'20090501',1,1000 Union All Select 2,'20090501',1,2000 Union All Select 3,'20090501',1,3000 Union All Select 1,'20090501',2,-25 Union All Select 2,'20090501',2,-25 Union All Select 3,'20090501',2,-25 Union All Select 1,'20090501',3,20 Union All Select 2,'20090501',3,10 Union All Select 3,'20090501',3,10 Union All Select 1,'20090501',4,150 Union All Select 2,'20090501',4,20 Union All Select 3,'20090501',4,150 Union All Select 1,'20090501',5,-62 Union All Select 2,'20090501',5,-62 Union All Select 3,'20090501',5,-62 Union All Select 1,'20090501',6,0 Union All Select 2,'20090501',6,0 Union All Select 3,'20090501',6,0 Union All Select 1,'20090501',7,0 Union All Select 2,'20090501',7,0 Union All Select 3,'20090501',7,0 Union All Select 1,'20090501',8,0 Union All Select 2,'20090501',8,0 Union All Select 3,'20090501',8,0 Union All Select 1,'20090501',9,0 Union All Select 2,'20090501',9,0 Union All Select 3,'20090501',9,0Go
建测试存储过程:
Use testGOIf object_id('up_SalarySUM') Is Not Null Drop Proc up_SalarySUMGoCreate Proc up_SalarySUM( @SalaryMonth datetime)AsSet Nocount OnDeclare @SalaryItemID nvarchar(50), @ItemType smallint, @Sql nvarchar(4000), @Definition nvarchar(4000), @Cols nvarchar(4000)If object_id('tempdb..#Salary') Is Not Null Drop Table #SalaryCreate Table #Salary(EmployeeID int,DimissionDate datetime,SalaryMonth datetime)Select @Cols=Isnull(@Cols+',','')+Rtrim(Quotename(a.ID)) From SalaryItem As a Inner Join Formulary As b On b.SalaryItemID=a.ID Where b.EffectiveDate<=@SalaryMonth And b.ExpiryDate>@SalaryMonthSet @Sql=N'Alter Table #Salary Add '+Replace(@Cols,',',' money,')+' money'Exec sp_executesql @SqlSet @Sql=N';With CTE_Salary As( Select a.EmployeeID,b.DimissionDate,SalaryMonth,a.SalaryItemID,a.Amount From Salary As a Inner Join Employee As b On b.ID=a.EmployeeID Where SalaryMonth In(@SalaryMonth,Dateadd(month,-1,@SalaryMonth)))Insert Into #Salary Select * From CTE_Salary Pivot(Max(Amount) For SalaryItemID In('+@Cols+')) As b'Exec sp_executesql @Sql,N'@SalaryMonth datetime',@SalaryMonthDeclare Cur_SalaryItem Cursor For Select Quotename(a.ID) As ID,a.ItemType,b.Definition From SalaryItem As a Inner Join Formulary As b On b.SalaryItemID=a.ID Where b.EffectiveDate<=@SalaryMonth And b.ExpiryDate>@SalaryMonth And a.ItemType In(1,4) Order By b.Sequence For Read OnlyOpen Cur_SalaryItemFetch Next From Cur_SalaryItem Into @SalaryItemID,@ItemType,@DefinitionWhile @@Fetch_status=0Begin If @ItemType=4 --本月可计算的工资项 Set @sql='Update #Salary Set '+@SalaryItemID+'='+@Definition +' Where SalaryMonth=@SalaryMonth' Else --计算上月工资项 Set @sql='Update a Set a.'+@SalaryItemID+'=(Select '+@Definition +' From #Salary Where EmployeeID=a.EmployeeID And SalaryMonth=Dateadd(Month,-1,a.SalaryMonth)) From #Salary As a Where SalaryMonth=@SalaryMonth' Exec sp_executesql @sql,N'@SalaryMonth datetime',@SalaryMonth Fetch Next From Cur_SalaryItem Into @SalaryItemID,@ItemType,@DefinitionEndClose Cur_SalaryItemDeallocate Cur_SalaryItemSet @sql=(N';With CTE_Salary As( Select * From #Salary Unpivot(Amount For SalaryItemID In('+@Cols+')) As up Where SalaryMonth=@SalaryMonth)Update a Set a.Amount=b.Amount From Salary As a Inner Join CTE_Salary As b On b.SalaryMonth=a.SalaryMonth And b.EmployeeID=a.EmployeeID And b.SalaryItemID=a.SalaryItemID')Exec sp_executesql @sql,N'@SalaryMonth datetime',@SalaryMonthDrop Table #SalaryGo
--执行工资计算
Exec up_SalarySUM '20090401'Exec up_SalarySUM '20090501'Go
---查看计算结果
Declare @Cols nvarchar(4000), @SalaryMonth datetime, @Sql nvarchar(4000)Set @SalaryMonth='20090501'Select @Cols=Isnull(@Cols+',','')+Rtrim(Quotename(a.Name)) From SalaryItem As a Inner Join Formulary As b On b.SalaryItemID=a.ID Where b.EffectiveDate<=@SalaryMonth And b.ExpiryDate>@SalaryMonthSet @Sql=N';With CTE_Salary As( Select a.EmployeeID,b.DimissionDate,SalaryMonth,c.Name As SalaryItem,a.Amount From Salary As a Inner Join Employee As b On b.ID=a.EmployeeID Inner Join SalaryItem As c On c.ID=a.SalaryItemID Where SalaryMonth=@SalaryMonth) Select * From CTE_Salary Pivot(Max(Amount) For SalaryItem In('+@Cols+')) As b'Exec sp_executesql @Sql,N'@SalaryMonth datetime',@SalaryMonthGo
Demo效果如图:
SQL server 自定义工资公式设计的更多相关文章
- sql server 自定义函数的使用
sql server 自定义函数的使用 自定义函数 用户定义自定义函数像内置函数一样返回标量值,也可以将结果集用表格变量返回 用户自定义函数的类型: 标量函数:返回一个标量值 表格值函数{内联表格值函 ...
- SQL Server:OA权限管理设计的实现 下
SQL Server:OA权限管理设计的实现 下 OA系统权限管理设计方案 不同职责的人员,对于系统操作的权限应该是不同的.优秀的业务系统,这是最基本的功能. 可以对“组”进行权限 ...
- 13、SQL Server 自定义函数
SQL Server 自定义函数 在SQL Server中不仅可以使用系统函数(如:聚合函数,字符串函数,时间日期函数等)还可以根据需要自定义函数. 自定义函数分为标量值函数和表值函数. 其中,标量值 ...
- SQL Server 自定义函数(Function)——参数默认值
sql server 自定义函数分为三种类型:标量函数(Scalar Function).内嵌表值函数(Inline Function).多声明表值函数(Multi-Statement Functio ...
- 也来谈谈SQL SERVER 自定义函数~
在使用SQL SERVER 数据库的时候,函数大家都应该用过,简单的比如 系统聚合函数 Sum(),Max() 等等.但是一些初学者编写自定义函数的时候,经常问起什么是表值函数,什么是标量值函数. 表 ...
- SQL Server自定义函数( 转载于51CTO )
用户自定义函数自定义函数不能执行一系列改变数据库状态的操作,可以像系统函数在查询或存储过程等的程序中使用,也可以像相信过程一样能过 execute 命令来执行.自定义函数中存储了一个 Transact ...
- 通过SQL Server自定义数据类型实现导入数据
写在前面 在看同事写的代码时看到了SQL Server中可以自定义数据类型,而且定义的是DataTable类型的数据类型. 后我想起了以前我们导入数据时要么是循环insert写入,要么是SqlBulk ...
- SQL Server 自定义聚合函数
说明:本文依据网络转载整理而成,因为时间关系,其中原理暂时并未深入研究,只是整理备份留个记录而已. 目标:在SQL Server中自定义聚合函数,在Group BY语句中 ,不是单纯的SUM和MAX等 ...
- Sql Server 自定义数据类型
SQLServer 提供了 25 种基本数据类型: ·Binary [(n)] 二进制数据 既可以是固定长度的(Binary),也可以是变长度的.其中,n 的取值范围是从 1 到 8000.其存储窨 ...
- SQL Server 自定义快捷键
SQL Server程序员经常要在SSMS(SQL Server Management Studio)或查询分析器(2000以前)中编写T-SQL代码.以下几个技巧,可以提升工作效率. 以下说明以SS ...
随机推荐
- 【Oracle】手动安装和卸载Oracle,这是最全的一篇了!!
写在前面 之前写过一篇在CentOS 7/8上安装Oracle的文章,按照我写的文章安装成功了,但是卸载Oracle时出现了问题.今天,我就整理一篇手动安装和卸载Oracle的文章吧.全文为实战型内容 ...
- YOLOv1/v2/v3简述 | 目标检测
YOLO系列是目标检测领域里十分经典的结构,虽然目前已经出了很多更高质量更复杂的网络,但YOLO的结构依然可以给算法工程师们带来很多的启发.这3篇论文看下来,感觉像是一本调参说明书,教你如何使用各种t ...
- 大模型提示工程之Prompt框架和示例
今天和大家分享一下:大模型提示工程之Prompt框架和示例: TAG框架 任务(Task): 开发一个新的手机应用,旨在帮助用户更好地管理他们的日常健康. 行动(Action): 进行市场调研,设计用 ...
- Scala编译原理
1 package com.atguigu.chapter01; 2 /** 3 * 4 */ 5 //main 方法名 6 //小括号表示参数列表 7 // 参数声明方式: java -> 类 ...
- JDK14中的java tools简介
目录 故事发生了 java tools简介 jaotc jar jarsigner java javac javadoc javap jcmd jconsole jdb jdeprscan jdeps ...
- Java HashMap 和 HashSet 的高效使用技巧
Java HashMap HashMap 是一种哈希表,它存储键值对.键用于查找值,就像数组中的索引一样.HashMap 的优势在于它可以使用任何类型作为键,并且查找速度很快. 创建 HashMap ...
- SQL(Structured Query Language)简介和常见 SQL 命令示例
简介 SQL(Structured Query Language)是一种用于访问和操作关系型数据库的标准语言.它是一个功能强大的语言,用于执行各种数据库操作,包括检索数据.插入新记录.更新记录.删除记 ...
- Docker学习路线6:使用第三方镜像
第三方镜像是在Docker Hub或其他容器注册表上提供的预构建Docker容器镜像.这些镜像由个人或组织创建和维护,可以作为您容器化应用程序的起点. 查找第三方镜像 Docker Hub 是最大和最 ...
- 【FAQ】HarmonyOS SDK 闭源开放能力 —Map Kit
1.问题描述 在App中供用户在地图上选择地址,目前在使用Map Kit结合geolocationManager逆地理编码时获取的地址信息描述不准确,希望能提供相应的Demo参考. 解决方案 Demo ...
- python读取ini配置文件-configparser使用方法
我们在操作 ini 配置文件的时候 可以使用 Python 的 configparser 库 具体使用方法如下: from configparser import ConfigParser # 初始化 ...