2

I have a table with two columns with data like this:

1,2
1,3
1,4
2,1
2,2
3,1

I want to select just unique combinations, so out of those I would end up with:

1,2
1,3
1,4
2,2

because 1,2 is the same combination as 2,1 etc

How would I go about that in a SQL statement?

In reality, my table has a third column and I want to add a where clause based on that third column so that only those rows are considered

2
  • 6
    Show us what you have tried. Commented Oct 19, 2012 at 14:38
  • 2
    @Mark I dont mean to bash anymore, but he was right, your query did not go close on solving the OP's problem, i believe it's more important to try your solutions to see if at least you can get the same resultset as the OP asked for Commented Oct 19, 2012 at 15:03

4 Answers 4

10
SELECT * FROM (
    SELECT
        CASE WHEN Col1 <= Col2 THEN Col1 ELSE Col2 END AS Col1,
        CASE WHEN Col1 <= Col2 THEN Col2 ELSE Col1 END AS Col2
    FROM
        MyTable
) Ordered
GROUP BY
    Col1, Col2

You could do it without the subquery by GROUPing on the CASE expressions, but it's longer to read.

Sign up to request clarification or add additional context in comments.

7 Comments

This works for the simple case I have put in my question, but when I add a where clause is doesn't - where would I put the where clause(s)
@Graham if you want a solution that works for your scenario, please post your actual scenario in the question. Dumbing it down clearly doesn't help anyone.
"It doesn't work" doesn't explain the problem enough. You need to elaborate on your input, expected and actual outcomes.
fair point, I have edited my original question. I didn't think it would make any difference, but it clearly does
With your condition, you should filter in the innermost SELECT.
|
7

Another way to achieve the same thing:

SELECT a, b
FROM tableX
WHERE a <= b
  AND (other conditions)

UNION 

SELECT b, a
FROM tableX 
WHERE a > b 
  AND (other conditions) ;

This variation may be different (regarding efficiency), depending on the indexes you have:

SELECT *
FROM
  ( SELECT a, b
    FROM tableX
    WHERE (other conditions)
  UNION 
    SELECT b, a
    FROM tableX 
    WHERE (other conditions)
  ) AS tmp
WHERE a <= b ;

7 Comments

This works, but without the distinct on SQLServer 2008. The execution plan shows AakashM's answer as faster (45% over 55%)
On my real data (with the where clause) AakashM gets 32% over 68%, but I have to admit I understand yours :-)
@Graham much more meaningful to measure duration, cpu, I/O etc. rather than the estimated cost percentage in the plan. I've seen plans with lower estimated cost take 10x as long to run - even when the estimated cost has come from an actual plan. Have you looked at SQL Sentry Plan Explorer?
The second method is the same efficiency as the first (on the real data)
@Graham there is a free version, did you look closely? There are two versions of Plan Explorer: FREE and PRO. The pro version has some additional features but they're not needed for basic performance comparisons.
|
-1

You can try something like:

select distinct col1, col2 from table
where col2 + '-' + col1 not in (select col1 + '-' + col2 from your_table)

Notice that you have to concatenate the fields and it depends of the column type (col1 + '-' + col2 works well with char and varchar types)

7 Comments

This, if it works, is taking far to long to run - still waiting for results
This can fail for some results, since it will see the row 23,1 as the same that 2,31
Now it will fail for "5-0,x" and "5,0-x". SQL Server has ways to determine unique without relying on concatenation, which requires a whole bunch of assumptions about the data in order to be trusted. Note that the column is clearly not an integer otherwise it would fail with conversion errors...
obviously you have to determine the separator character(s) according to your stored data, and make the conversion depending of the data type of your columns in order to concatenate the column values
e.g. in Oracle you can use a syntax like where col2, col1 not in (select col1, col2 from your_table) but you can't do that in SQL Server
|
-9

How about:

SELECT
      COL1, COL2, COUNT(*)
FROM
     Your_Table
GROUP BY
      COL1, COL2

5 Comments

This isn't what op wants, he needs that the combinations of Col1-Col2 or Col2-Col1 are unique
How is this even a solution? What do you achieve with the count?
How does my solution NOT anwser the OP's question: sqlfiddle.com/#!3/d8f44/1
Well, your result has nothing to do with the result set asked for. So, it doesn't answer his question at all
Hey mark I checked your SQL fiddle and it has results (1,2) and (2,1) which is exactly what OP said he was looking to avoid. Also why would @Lamak post another answer when AakashM already has the correct answer on the board. Calm down fella. Just because your particular solution will not work is no reason to get defensive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.