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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 2 ROW_NUMBER() problems

Author  Topic 

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2011-03-11 : 22:33:09
Hi i'm trying a row_number.
However there are 3 problems.
1)If i use count i get only 1 count line (so every row show 1)
Ok that is the least of my problems i can count the rows inside asp.net
now:
2)I want to use order by and when i use it what i get is rownumbers mixed.So i rarely will get 1,2,3,4,5... on rownum.I usually will get 1,2,5,4,3 etc.Please view the order by i have, it's for different order by's per @variable
3)
if i put on the first code row
select @r=rowrank from(
and then the query,i get:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

Ah, also if i have p.e. 5 rows and i use top 3 then again the rownumber will be their rows as if they were in a full row combination.
So p.e. if the rows come like 2,3,4,5,1 and i do a top 3 i will get
2,3,4 instad of 1,2,3 but i suspect this is due to the 2nd question.

Here is my code:
select  id,countanimals,chipid,animalsname,userfixeddate,rowrank
from
(
SELECT top 100 animals.id,count(animals.id) as countanimals,animals.chipid as chipid,
animals.name as animalsname,animals.userfixeddate,
ROW_NUMBER()
OVER (
order by CASE WHEN @orderbyc=1 THEN animals.name
END ASC,
CASE WHEN @orderbyc=2 THEN animals.name
END DESC,
userfixeddate ASC
) as 'ROWRANK'

FROM Animals ON Animals.ownerid = Owners.id INNER JOIN
pettype ON Animals.pettype = pettype.id
where animals.usname=@usnamelower and (animals.lag <> 99 or animals.lag is null)
and (nullif(@animalname,'') is null or animals.name like @animalname + '%')
group by animals.id,chipid,animals.name,animals.userfixeddate,animals.createddate
order by animals.createddate asc
)t
group by t.id,t.countanimals,t.chipid,t.animalsname,t.userfixeddate,t.rowrank
order by
CASE WHEN @orderbyc=1 THEN animalsname
END ASC,
CASE WHEN @orderbyc=2 THEN animalsname
END DESC,
userfixeddate ASC

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2011-03-11 : 22:39:21
To help.
I removed the count so i also removed the group by,
then i also removed the outer order by (outside the "t").
I still get 2,3,1,4,5... etc
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2011-03-11 : 22:45:22
Aha, now i'm getting somewhere.
I removed also
order by animals.createddate asc
inside t and it seems i get the correct count.
But that is not what i want.
I have this so if any same animals.name and userfixeddate match then
.createddate will order them...

Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2011-03-11 : 23:10:14
Ok if i put createddate asc just before ') as ROWRANK' and after userfixeddate ASC rowrank does not change and also top works ok.
So the questions i have is how can i use the count() correctly and why i
can't use
select @r=rowrank from(
Go to Top of Page
   

- Advertisement -