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
 General SQL Server Forums
 New to SQL Server Programming
 help

Author  Topic 

mani_1234
Starting Member

24 Posts

Posted - 2012-09-30 : 23:44:33
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)

17689 Posts

Posted - 2012-10-01 : 00:18:13
[code]
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
[/code]


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

Go to Top of Page

mani_1234
Starting Member

24 Posts

Posted - 2012-10-01 : 01:03:09
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

52326 Posts

Posted - 2012-10-01 : 10:56:13
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
   

- Advertisement -