Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hisouka
Starting Member
28 Posts |
Posted - 2009-05-18 : 05:22:09
|
| Hi, i want to make a function or procedure with this scenario:EmployeesTableID, Name, AgeEstimateTableEstimateID, EmployeeID, Date, EstimateTypeI want to select All Employee with their specific estimate.Each employee have Multiple Entry in EstimatetableI only want one to return ID, Name, Age, and only one EstimateType.Criteria for EstimateType:Most Recent Estimate of EmployeeIf Date is null it must Return EstimateType=ContractPrice which is you can only add one ContractPrice Type.Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-18 : 05:27:41
|
[code]select e.ID, e.Name, e.Age, e.EstimateTypefrom( select e.ID, e.Name, e.Age, EstimateType = case when s.[Date] is null then 'ContractPrice' else s.EstimateType end, row_no = row_number() over (partition by e.ID order by s.[Date] desc) from EmployeesTable e inner join EstimateTable s on e.ID = s.EmployeeID) ewhere e.row_no = 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
hisouka
Starting Member
28 Posts |
Posted - 2009-05-18 : 05:32:19
|
quote: Originally posted by khtan
select e.ID, e.Name, e.Age, e.EstimateTypefrom( select e.ID, e.Name, e.Age, EstimateType = case when s.[Date] is null then 'ContractPrice' else s.EstimateType end, row_no = row_number() over (partition by e.ID order by s.[Date] desc) from EmployeesTable e inner join EstimateTable s on e.ID = s.EmployeeID) ewhere e.row_no = 1 KH[spoiler]Time is always against us[/spoiler]
Hi khtan, thanks for the reply. One more question, is the query will take minimum time even if the record is 1Million?Thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-18 : 05:42:35
|
i don't have 1 million records to try. Why don't you try it out ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
hisouka
Starting Member
28 Posts |
Posted - 2009-05-18 : 05:44:26
|
| i receive error. Invalid column name 'row_no'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-18 : 06:18:55
|
post the query that you used KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|