SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mani_1234
Starting Member

India
24 Posts

Posted - 09/30/2012 :  23:44:33  Show Profile  Reply with Quote
i have input table like this

a b sq_no
a1 b1 1
b1 a1 2
a2 b2 3
c2 d2 4
c1 d1 5
d1 c1 6

i want output like this which have unique combination only with desc. order wrt to sq_no

a b sq_no
d1 c1 6
c2 d2 4
a2 b2 3
b1 a1 2

khtan
In (Som, Ni, Yak)

Singapore
17600 Posts

Posted - 10/01/2012 :  00:18:13  Show Profile  Reply with Quote

select	*
from
(
	select	*,
		rn = row_number() over ( partition by case when a < b then a + b else b + a end order by sq_no desc)
	from	input
) d
where	d.rn	= 1
order by sq_no desc



KH
Time is always against us

Go to Top of Page

mani_1234
Starting Member

India
24 Posts

Posted - 10/01/2012 :  01:03:09  Show Profile  Reply with Quote
thnx bt it can b more optimized if possble in terms of io and time
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/01/2012 :  10:56:13  Show Profile  Reply with Quote
quote:
Originally posted by mani_1234

thnx bt it can b more optimized if possble in terms of io and time



do you mean current its taking lots of io?

can you check execution plan and see what are costly steps?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000