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)
 Count Query

Author  Topic 

mapidea
Posting Yak Master

124 Posts

Posted - 2009-01-31 : 23:51:29
What does the following query returns

Select * from Products p1
Where 3 = ( Select Count(*) from Products p2 where p1.Price > p2.price)

Thanks

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-01 : 00:21:23
It should give everything including your 4th Highest price in Product table.
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-01 : 00:31:35
In SQL 2005 It will be:

Select * from
(Select Dense_Rank() over( order by Price desc) as ID
,* from dbo.Products)Z
Where Z.ID =4
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-01 : 01:01:15
[code]Select Columns from
(Select Columns,Price,(Select Count(distinct price) from Products Where Price >= t.Price)as ROW_ID
from Products t)Z
Where Z.ROW_ID= 4[/code]
Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2009-02-02 : 10:29:35
Thanks
Go to Top of Page
   

- Advertisement -