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)
 sql get common and median

Author  Topic 

jhnroze
Starting Member

1 Post

Posted - 2010-12-22 : 04:17:43

how do i write this in sql with the following criteria
note:its always three values since im only getting the top 3

criteria :

rows
p1
p2
p3


or


p3
p2
p1


ans:get p2

criteria
rows
p1
p2
p1

or

rows
p2
p1
p1

or
rows
p1
p1
p2

ans:get the common p1

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-12-22 : 04:49:50
quote:
Originally posted by jhnroze


how do i write this in sql with the following criteria
note:its always three values since im only getting the top 3

criteria :

rows
p1
p2
p3


or


p3
p2
p1


ans:get p2




Use row_number function and take the second one. It will get you p2 for whatever order by you do

quote:

criteria

rows
p1
p2
p1

or

rows
p2
p1
p1

or
rows
p1
p1
p2

ans:get the common p1




Use count method and display the id having maximum count.

Go to Top of Page
   

- Advertisement -