简体   繁体   中英

Comparing the same row in SQL Server table

How to display the result like India Vs Pakistan, India Vs Sri Lanka, India Vs Bangladesh, Bangladesh Vs Sri Lanka, Bangladesh Vs Pakistan, Pakistan Vs Sri Lanka from the below table?

Teams
-------
India
Pakistan
Sri Lanka
Bangladesh

For Example:

--Output:
Bangladesh vs. India
Bangladesh vs. Sri Lanka
Bangladesh vs. Pakistan
India vs. Sri Lanka
India vs. Pakistan
Pakistan vs. Sri Lanka

I guess you are looking for non repeating combinations of team matches. Try below:

CREATE TABLE temp
(
    Team VARCHAR(100)
)


INSERT INTO temp
VALUES ('India'),
('Bangladesh'),
('Sri Lanka'),
('Pakistan')


SELECT  t1.team + ' vs. ' + t2.team
FROM temp t1
CROSS JOIN  temp t2 
where t1.Team < t2.Team


DROP TABLE temp

    Result
    -----------
    India vs. Sri Lanka
    India vs. Pakistan
    Bangladesh vs. India
    Bangladesh vs. Sri Lanka
    Bangladesh vs. Pakistan
    Pakistan vs. Sri Lanka

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM