question

ranjanjena17 avatar image
ranjanjena17 asked

How to get the below result.?

I have table called : Tab1 , having column col1

Col1

A

B

C

D

Need a generic query to fetch below result

AB

AC

AD

BC

BD

CD

sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered
declare @Tab1 table (col1 char(1))
insert into @Tab1 (col1)
values ('A'),('B'),('C'),('D')


select Tab1_1.col1 + Tab1_2.col1
from @Tab1 Tab1_1
join @Tab1 Tab1_2 on Tab1_2.col1 > Tab1_1.col1
order by Tab1_1.col1

----
AB
AC
AD
BC
BD
CD

(6 rows affected)

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

As a bit of a sidebar, this is a classic example of an "Exclusive Triangular Join".

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.