tags:

views:

160

answers:

5

What is an SQL command that checks for rows that have rows with no duplicate fields in them.

ex:

A A A B B B should not be in the resulting table.

Only rows such as A B C D E F

i.e. given data like:

A A A B B B

A B C D E F

A A B G H Q

Should return A B C D E F

A: 
SELECT DISTINCT * FROM tablename
Thomas O
A: 

SELECT DISTINCT col FROM tabl

Novikov
No, this would would still return rows with duplicate values IN THEM. I want DISTINCT VALUES IN THE ROWS, NOT THE TABLE.
AnEventHorizon
KEEP USING CAPS IT MAKES THE TEXT MORE READABLE. Oh wait, it doesn't.
Thomas O
It's called emphasizing.
AnEventHorizon
*it's easier to use this.* (just use asterisks.)
Thomas O
@AnEventHorizon: most people will interpret all-caps text as yelling. That is something you may want to keep in mind when replying to posts made by people trying to lend you a hand.
Martin Törnwall
A: 

SELECT * FROM mytable

WHERE mytable.col1 != mytable.col2 != mytable.col3 ...

kofucii
this query will not run. You have to compare values one by one. also you should use <> instead of !=
Kamyar
This is just the principle. Real implementation depends on OP DB server. And why do you think != is wrong. For AND clause I'm agree.
kofucii
+2  A: 

Select distinc * returns unique ROWS not unique values from fields. You should compare each column's value with others. (Assuming column types are the same). For example, for a 4 column table you should do smoething like:

  SELECT Col1, Col2, Col3, Col4 FROM MyTable WHERE
  Col1 NOT IN (Col2,Col3,Col4) AND
  Col2 NOT IN (Col3,Col4) AND
  Col3 <> Col4
Kamyar
Probably easier to use SELECT Col1, Col2, Col3, Col4 FROM MyTable WHERE Col1 IN (Col2, Col3, ...) AND Col2 IN (Col1, Col3, ...) ...
Thomas O
Yes, this is correct. But I don't want this as it is a lot of code hassle. I am asking if there is an SQL command that checks your table for unique in rows and returns it.
AnEventHorizon
Agreed. just change it to: WHERE Col1 **NOT** IN (Col2, Col3, ...)
Kamyar
So there are no 1 line commands that do this?
AnEventHorizon
@AnEventHorizon: Not that I know of. probably not at all.
Kamyar
+3  A: 

There is no simple command to do this.

is seems an unusual requirement and possibly an indication that the table is not in first normal form if all columns are interchangeable.

The following works in Microsoft SQL Server

;With YourData AS
(
select 'A' as C1, 'A' as C2, 'A' as C3, 'B' as C4,  'B' as C5,  'B' as C6 UNION ALL
select 'A' as C1, 'B' as C2, 'C' as C3, 'D' as C4,  'E' as C5,  'F' as C6
)
SELECT *
FROM   YourData
WHERE  1 =
       ( SELECT  TOP 1 COUNT(*) AS Cnt
       FROM     ( 
                SELECT C1 AS     C
                UNION ALL
                SELECT C2
                UNION ALL
                SELECT C3
                UNION ALL
                SELECT C4
                UNION ALL
                SELECT C5
                UNION ALL
                SELECT C6
                ) D
       GROUP BY C
       ORDER BY Cnt DESC
       )
Martin Smith
+1 the table must be normalized. nice work with the query by the way ;)
MartinodF