我有这个T-SQL查询,我想优化。
我不知道如何使所有其余的查询从"where DateLoad...."应该修改为。
看来我有可能使它更简单,但我不确定如何使它更简单。
select Employee_Number,
DateLoad,
min(Rate_1_Pay_Rate) as Rate_1_Pay_Rate,
min(Hourly_Rate) as Hourly_Rate,
min(FLSA_Status) as FLSA_Status,
min(Hire_Date) as Hire_Date,
min(Employee_Type) as Employee_Type,
min(Status_Type) as Status_Type
from dbo.TableMain as hist
where DateLoad = (select min(DateLoad)
from dbo.TableMain as hist2
where hist.Employee_Number = hist2.Employee_Number)
and Employee_Type = (select min(Employee_Type)
from dbo.TableMain as hist2
where hist.Employee_Number = hist2.Employee_Number)
and Status_Type = (select min(Status_Type)
from dbo.TableMain as hist2
where hist.Employee_Number = hist2.Employee_Number)
and Hire_Date = (select min(Hire_Date)
from dbo.TableMain as hist2
where hist.Employee_Number = hist2.Employee_Number)
group by Employee_Number, DateLoad
如果不了解数据的更多上下文,我认为您可能无法真正获得基于5个单独列中的每个列的最小值的预期输出。让我们用一些数据来演示一下
TableMain
Employee_Number DateLoad Employee_Type Status_Type Hire_Date
1 1/25 Z D 1/21
1 2/15 Y A 1/21
1 6/21 X B 1/21
通过查看单个雇员的示例数据(看起来您的表将为单个人有多个条目),如果我对每个相应列取MIN(),我将得到
Minimums MINIMUM MINIMUM MINIMUM MINIMUM
Employee_Number DateLoad Employee_Type Status_Type Hire_Date
1 1/25 X A 1/21
通过连接返回找到"single";包含这5个元素的记录不存在。
请编辑你的帖子,试着提供样本数据,以更好地说明你有数据和你正在寻找从它得到什么。简单的英文描述,以澄清,但编辑你的原始帖子,并提供额外的数据和背景。