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)
 duplicated with criteria...

Author  Topic 

hai
Yak Posting Veteran

84 Posts

Posted - 2008-06-26 : 21:44:30
id name qty amount
1 user1 3 0
2 user2 3 0
3 user1 2 3
4 user3 2 0
5 user3 2 1
6 user2 1 1
7 user1 2 3
8 user3 3 0
9 user1 3 3
result
id name
9 user1
6 user2
5 user3

select * from
(
select *, rank() over (Partition BY name order by qty DESC, amount desc) as Rankid
from
table1
) tbl1
where rankid=1

this is not giving me the result. Any help is greatly appreciate.

thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-26 : 22:12:45
your ranking is by qty desc, amount desc. for user2 that will give you id = 2. Why are you expecting id = 6 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

hai
Yak Posting Veteran

84 Posts

Posted - 2008-06-26 : 22:17:32
Because the amount is 0, although the qty is 3, if amount is zero, then go to the next one.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-26 : 22:24:39
quote:
Originally posted by hai

Because the amount is 0, although the qty is 3, if amount is zero, then go to the next one.



"Partition BY name order by qty DESC, amount desc"

So what is the rule that you want ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

hai
Yak Posting Veteran

84 Posts

Posted - 2008-06-26 : 22:42:51
if qty have amount other than 0 select the highest qty, otherwise select the lower qty that have amount. if all the qty have 0 amount, than select the highest qty for the same name.I know my query is not correct, and thank you for helping.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-26 : 23:02:35
[code]
Partition BY name
order by case when amount <> 0 then 1 else 2 end,
qty desc,
amount desc

[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -