比较两个表的SQL Server 2005 [英] compare two tables sql server 2005

查看:128
本文介绍了比较两个表的SQL Server 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Frds,我有三个表,一个是员工明细,另一个是薪水明细,另一个是给定薪水明细..

表1:员工详细信息

Emp_Id Emp_Name
1 A
2 B
3 C
4 D
5 E
6 F
7克
8小时
9 J
10 K


表2:Salary_Allocation
Emp_Id Salary_Month Salary_Amount
10000年1月1日
9000年1月2日
9000年1月3日
9000年1月4日
9000年1月5日
9000年1月6日
9000年1月7日
8000年1月8日
8500年1月9日
8500年1月10日
11000年2月1日
10000年2月2日
10000年2月3日
10000年2月4日
9500年2月5日
9500年2月6日
9500年2月7日
8500年2月8日
9000年2月9日
8500年2月10日


表3:Salary_Deposit

Emp_Id Salary_Month Salary_Deposit

10000年1月1日
9000年1月2日
7000年1月3日
7000年1月4日
7000年1月5日
9000年1月6日
7000年1月7日
6000年1月8日
6500年1月9日
6500年1月10日
9000年2月1日
8000年2月2日
8000年2月3日
10000年2月4日
7500年2月5日
7500年2月6日
9500年2月7日
8500年2月8日
7000年2月9日
8500年2月10日


通过比较上面的三个表...我想知道哪个雇员哪个月的薪水待定...


我需要如下输出


Emp_Id Salary_Month Salary_Pending

2000年1月3日
2000年1月4日
2000年1月5日
2000年1月7日
2000年1月8日
2000年1月9日
2000年1月10日
2000年2月1日
2000年2月2日
2000年2月3日
2000年2月5日
2000年2月6日
2000年2月9日

Hi Frds I have three tables one is employee Detail,another is salary detail and other is salary given details..

Table 1:Employee Detail

Emp_Id Emp_Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 J
10 K


Table 2: Salary_Allocation
Emp_Id Salary_Month Salary_Amount
1 Jan 10000
2 Jan 9000
3 Jan 9000
4 Jan 9000
5 Jan 9000
6 Jan 9000
7 Jan 9000
8 Jan 8000
9 Jan 8500
10 Jan 8500
1 Feb 11000
2 Feb 10000
3 Feb 10000
4 Feb 10000
5 Feb 9500
6 Feb 9500
7 Feb 9500
8 Feb 8500
9 Feb 9000
10 Feb 8500


Table 3:Salary_Deposit

Emp_Id Salary_Month Salary_Deposit

1 Jan 10000
2 Jan 9000
3 Jan 7000
4 Jan 7000
5 Jan 7000
6 Jan 9000
7 Jan 7000
8 Jan 6000
9 Jan 6500
10 Jan 6500
1 Feb 9000
2 Feb 8000
3 Feb 8000
4 Feb 10000
5 Feb 7500
6 Feb 7500
7 Feb 9500
8 Feb 8500
9 Feb 7000
10 Feb 8500


by comparing above three tables ...I want to know which employee for which month how much salary is pending...


I need output as follows


Emp_Id Salary_Month Salary_Pending

3 Jan 2000
4 Jan 2000
5 Jan 2000
7 Jan 2000
8 Jan 2000
9 Jan 2000
10 Jan 2000
1 Feb 2000
2 Feb 2000
3 Feb 2000
5 Feb 2000
6 Feb 2000
9 Feb 2000

推荐答案

假设每个员工每月只有一条记录,则该记录应该起作用:
Supposing that there is only one record per employee per month, this one should work:
select e.*, a.Salary_Amount - d.Salary_Deposit as Salary_Pending
from
Employee_Detail e ,Salary_Allocation a, Salary_Deposit d
where
e.Emp_id = a.Emp_id
and
e.Emp_id = d.Emp_id
and
a.Salary_Month = d.Salary_Month
and
a.Salary_Amount - d.Salary_Deposit <> 0


这篇关于比较两个表的SQL Server 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆