March 20, 2012 at 12:45 am
Hi,
create table student(s_id number,s_name varchar2(20),marks number);
insert into student values(1,'XXX',1000);
insert into student values(2,'YYY',3999);
insert into student values(7,'YY',999);
so on for n number of records....
o/p:
s_id marks somecolname
2 3999 1
1 1000 2
7 999 3
Thanks in Advance..
March 20, 2012 at 12:49 am
select *,ROW_NUMBER() OVER (ORDER BY marks desc)ranks from student
Regards
Guru
March 20, 2012 at 12:52 am
Hi Thanks,
I need the result without using any predefined functions like row_number(),rank(),dense_rank(),ntile()..
March 20, 2012 at 1:02 am
Hi thanks got it..
select
empno,
ename,
deptno,
sal,
(select count(sal)+1 from emp b where b.deptno=a.deptno and b.sal < a.sal) rnk
from
emp a
order by
deptno, sal;
March 20, 2012 at 1:12 am
create table #temp (ranks int identity ,s_id int,s_name varchar(20),marks numeric(9,0))
Insert into #temp select * from student order by marks desc
select * from #temp
drop table #temp
March 20, 2012 at 2:23 am
Why can't you use the windowing functions? Homework limitation?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 20, 2012 at 7:14 pm
nkcherukuri (3/20/2012)
Hi thanks got it..select
empno,
ename,
deptno,
sal,
(select count(sal)+1 from emp b where b.deptno=a.deptno and b.sal < a.sal) rnk
from
emp a
order by
deptno, sal;
Please read the following article to see why that's probably the worst way to do such a thing.
http://www.sqlservercentral.com/articles/T-SQL/61539/
--Jeff Moden
Change is inevitable... Change for the better is not.
March 20, 2012 at 7:19 pm
GuruGPrasad (3/20/2012)
create table #temp (ranks int identity ,s_id int,s_name varchar(20),marks numeric(9,0))Insert into #temp select * from student order by marks desc
select * from #temp
drop table #temp
SELECT/INTO would be faster and cause less logging in TempDB.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 21, 2012 at 12:37 am
nkcherukuri (3/20/2012)
Hi Thanks,I need the result without using any predefined functions like row_number(),rank(),dense_rank(),ntile()..
Tell your professor/teacher that it is stupid to put such limitations on their assignments.
What does it teach? How to write inefficient code? The functions are there, use them.
(this reminds me of teachers saying: "you won't always have a calculator!". Euh, yes I do.)
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
March 21, 2012 at 12:48 am
Thank you Jeff Moden
March 21, 2012 at 3:30 am
Please read the following article to see why that's probably the worst way to do such a thing.
This woke me up from a nap after lunch...
Viewing 11 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply