案件是,我想使用SSRS进行报告,以比较员工2年之间的薪水。 这些年,员工人数和工资在1表中。
表结构是:
例如his_pay_period_num, his_employee_num, Salary
:
[薪酬]:
2007,A1234,$30000
2009,A1234,$50000
2009,B1234,$15000
我想输出:
A1234 , $30000, $50000 <-----how can I select 2 Salary with different input(2007,2009)?
B1234, $0, $15000 <----------- And if no Record,return 0
目前,我正在做:
SELECT t1.his_pay_period_num, t1.his_employee_num ,t1.Salary AS rate1,
t2.Salary AS rate2
FROM [Salary] as t1 Left outer join
[Salary] as t2 on t1.id = t2.id
WHERE (t1.his_pay_period_num = '2007') or (t2.his_pay_period_num= '2009')
输出:
A1234, $30000,$30000
A1234, $50000,$50000 <-----just display 2 record
B1234, $15000,$15000
问题1:如何选择不同输入的2个工资?
问题2:如果不存在记录,则返回0
答案 0 :(得分:1)
假设您的DBMS支持PIVOT
关键字,您可以执行以下操作:
select
his_employee_num,
coalesce([2007],0) as [Pay in 2007], --0 if first argument is NULL
coalesce([2009],0) as [Pay in 2009]
from
(select * from salary) s
pivot
(max(salary) for his_pay_period in ([2007],[2009])) p
这将适用于SQL Server和Oracle(可能需要稍微调整一下)。