| Author |
Topic |
|
Prabu.Ramaiah
Starting Member
15 Posts |
Posted - 2010-05-24 : 01:22:15
|
| HiI am having the belowEmpid Deptid Salary----- ------- ------ 1 10 100 2 20 200 3 30 300 4 40 400 5 10 100 6 20 200 7 30 300 8 40 400 9 50 500 10 10 100 11 20 200 12 30 300--------------------------I want to select the last two rows in dept wise like thisEmpid Deptid Salary----- ------- ------ 3 30 300 4 40 400 8 40 400 9 50 500 11 20 200 12 30 300--------------------------How to select please help me |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-05-24 : 02:27:17
|
| Did you mean for each department you want last two records.Karthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
Prabu.Ramaiah
Starting Member
15 Posts |
|
|
karthik_padbanaban
Constraint Violating Yak Guru
263 Posts |
Posted - 2010-05-24 : 02:31:33
|
Is this the one you want......?declare @a table (Empid int, Deptid int ,Salary int )insert into @a values (1, 10, 100)insert into @a values (2, 20, 200)insert into @a values (3, 30, 300)insert into @a values (4, 40, 400)insert into @a values (5, 10, 100)insert into @a values (6, 20, 200)insert into @a values (7, 30, 300)insert into @a values (8, 40, 400)insert into @a values (9, 50, 500)insert into @a values (10, 10, 100)insert into @a values (11, 20, 200)insert into @a values (12, 30, 300)select empid,deptid,salary from (select empid,deptid,salary,row_number()over(partition by deptid order by empid desc) as row from @a) a where row <=2empid deptid salary----------- ----------- -----------10 10 1005 10 10011 20 2006 20 20012 30 3007 30 3008 40 4004 40 4009 50 500 Karthikhttp://karthik4identity.blogspot.com/ |
 |
|
|
Prabu.Ramaiah
Starting Member
15 Posts |
Posted - 2010-05-24 : 02:39:38
|
| No, I want the below output, actually the deptid 10-40 one department, 10-50 another dept, 10-30 another deptmentEmpid Deptid Salary----- ------- ------3 30 3004 40 4008 40 4009 50 50011 20 20012 30 300-------------------------- |
 |
|
|
kashyapsid
Yak Posting Veteran
78 Posts |
Posted - 2010-05-24 : 03:26:09
|
| we can't understand wt you want the output?????KaShYaP |
 |
|
|
Prabu.Ramaiah
Starting Member
15 Posts |
Posted - 2010-05-24 : 03:29:36
|
| HiI am having the belowEmpid Deptid Salary----- ------- ------1 10 1002 20 2003 30 3004 40 4005 10 1006 20 2007 30 3008 40 4009 50 50010 10 10011 20 20012 30 300--------------------------I want to select the last two rows in dept wise like thisEmpid Deptid Salary----- ------- ------3 30 3004 40 4008 40 4009 50 50011 20 20012 30 300--------------------------How to select please help me |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-05-24 : 04:29:51
|
quote: Originally posted by Prabu.Ramaiah HiI am having the belowEmpid Deptid Salary----- ------- ------1 10 1002 20 2003 30 3004 40 4005 10 1006 20 2007 30 3008 40 4009 50 50010 10 10011 20 20012 30 300--------------------------I want to select the last two rows in dept wise like thisEmpid Deptid Salary----- ------- ------3 30 3004 40 4008 40 4009 50 50011 20 20012 30 300--------------------------How to select please help me
For dept 30 you have empid 3,7 & 12 and all have same salary. On what basis you are selecting 3 & 12 ? |
 |
|
|
naveengopinathasari
Yak Posting Veteran
60 Posts |
Posted - 2010-05-24 : 05:47:07
|
| Hi,Please Execute this query and check the Result.Create a Table With the Name EmpSal give the same column names as you have givenQuery to Fecth the result as you have mentionedas mentioned by you your result set doesnt match the logic you have given.as i understand you require to list dept wise last 2 rows (each dept last 2 rows).If so this query will return.----------------------------------------------------------------------------------------------Select * FROM (Select ES.EmpID , ES.DeptID , ES.Salary , EP.Cnt , NTILE(10) OVER(PARTITION BY ES.DeptID ORDER BY ES.DeptID DESC) AS RankRows FROM EmpSal ES, (Select DeptID , Count(DeptID) AS Cnt FROM EmpSal GROUP BY DeptID ) EPWHERE ES.DeptID = EP.DeptIDGROUP BY ES.EmpID , ES.DeptID , ES.Salary ,EP.Cnt ) FinalQryWHERE FinalQry.RankRows >FinalQry.Cnt-2 ---------------------------------------------------------------------------------------------- Query to Create the TableCREATE TABLE [dbo].[EmpSal]( [EmpID] [int] NULL, [DeptID] [int] NULL, [Salary] [int] NULL) ON [PRIMARY]Lets unLearn |
 |
|
|
|