tags:

views:

90

answers:

5

Probably it has been asked before but I cannot find an answer.

Table Data has two columns:

Source   Dest
1         2
1         2
2         1
3         1

I trying to come up with a MS Access 2003 SQL query that will return:

1       2
3       1

But all to no avail. Please help!

UPDATE: exactly, I'm trying to exclude 2,1 because 1,2 already included. I need only unique combinations where sequence doesn't matter.

A: 

Use this query :

SELECT distinct *  from tabval ;
pavun_cool
This is not what was asked for...
astander
A: 

To eliminate duplicates, "select distinct" is easier than "group by":

select distinct source,dest from data;

EDIT: I see now that you're trying to get unique combinations (don't include both 1,2 and 2,1). You can do that like:

select distinct source,dest from data
minus
select dest,source from data where source < dest

The "minus" flips the order around and eliminates cases where you already have a match; the "where source < dest" keeps you from removing both (1,2) and (2,1)

David Gelhar
will return 1,2 ; 2,1; 3;1 - 2,1 should be excluded
SQL n00b
You can't use minus in MS Access, hence the problems :)
Nick Craver
A: 

your question is asked incorrectly. "unique combinations" are all of your records. but i think you mean one line per each Source. so it is:

SELECT *
FROM   tab t1
WHERE  t1.Dest IN 
(
   SELECT TOP 1 DISTINCT t2.Dest
   FROM tab t2
   WHERE t1.Source = t2.Source
)
Andrey
Doesn't work in MS Access 2003, complains about IN clause. Btw, just in case there is a sample of desired output in the question to eliminate possible ambiguity.
SQL n00b
this query will return desired rows, but i have no idea how to write in in ms access sql.
Andrey
Andrey, apologies, I clarified within the question that it's got to be Access 2003.
SQL n00b
+1  A: 

For Ms Access you can try

SELECT  DISTINCT
        *
FROM Table1 tM
WHERE NOT EXISTS(SELECT 1 FROM Table1 t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)

EDIT:

Example with table Data, which is the same...

SELECT  DISTINCT
        *
FROM Data  tM
WHERE NOT EXISTS(SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)

or (Nice and Access Formatted...)

SELECT DISTINCT *
FROM Data AS tM
WHERE (((Exists (SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source))=False));
astander
First revision returns 1,2; 1,2; 3,1 which is obviously incorrect.
SQL n00b
I just run this on MS Access and it returned 1,2 3,1. Did you test this? Did you include the **DISTINCT** ?
astander
Astander, copied it one-to-one, just replaced Table1 with Data
SQL n00b
Astander mine is Access 2003, SP3
SQL n00b
DISTINCT should still work in Access 2003. That was not a new feature lately...
astander
DISTINCT works, revision 2 code still returns 1,2; 1,2; 3,1
SQL n00b
Please check your query properties that *Unique Values* is set to **Yes**.
astander
Astander, I guess that's * since I have columns other than Source and Dest. Specifying Source and Dest next to DISTINCT fixes it.
SQL n00b
Yes, that would be it. DISTINCT works on an entire row/selected fields.
astander
A: 
SELECT t1.* FROM
(SELECT
  LEAST(Source, Dest) AS min_val,
  GREATEST(Source, Dest) AS max_val
FROM table_name) AS t1
GROUP BY t1.min_val, t1.max_val

Will return

1, 2
1, 3

in MySQL.

True Soft