views:

88

answers:

7

Getting unique rows/values in SQL. I have same some data like this

valueA  ValueB    ValueC

test    "Value1"  "Something"

test    "Value1"  "Something else"

test2   "Value1"  "Something else"

So then I want to get the first two rows since valueC is different and then I also want row 3.

but say Row 4 is

test    "Value1"  "Something"

The same as Row 1. I don't want that row.

Can I do this with a select statement or where or something else?

In my schema ValueA is in one table and then Values b and C are from a different table. So I am doing something like

select * from table1,table2

which gives me the 3 values.

But I don't want the repeated values.

+1  A: 

Use SELECT DISTINCT ?

As in:

SELECT DISTINCT value1, value2, value3 FROM table1, table2
Gary McGill
I looked at distinct, like"Select Distinct ValueC from Table2"but then if you look at my example that would leave out row 3 where the ValueC is the same but Value A is not.
Maestro1024
Ah, but you need to put all the columns you want to be distinct in the SELECT clause - so, not SELECT DISTINCT ValueC, but SELECT DISTINCT ValueA, ValueB, ValueC.
Gary McGill
+1  A: 

Select Distinct * from table1, table2

Matt Hamsmith
A: 

How about this?

SELECT DISTINCT * FROM Table1, Table2
Steve Wortham
+1  A: 

You might want to have a look to the SQL reference for:

  • Unique
  • Distinct
  • Group By
Roberto Aloi
+1  A: 
SELECT DISTINCT *
FROM table1, table2
Maximilian Mayerl
A: 

Try this:

select a.valueA, b.valueB, b.valueC
from table1 a join table2 b on <join condition>
group by a.valueA, b.valueB, b.valueC;
wallenborn
+2  A: 
SELECT DISTINCT * FROM Table1, Table2

will do what you want.

However, your SELECT statement does not include an criteria to tell the system how to join the rows together. You will therefore get a cartesian product, containing a number of rows equal to the number of rows in Table1 times the number of rows in Table2. This is generally not what you want (occasionally it is).

So you probably want something like:

SELECT DISTINCT * FROM Table1, Table2 WHERE Table1.SomeColumn = Table2.SomeColumn

or, in a more current dialect of SQL:

SELECT DISTINCT * FROM Table1 INNER JOIN Table2 ON Table1.SomeColumn = Table2.SomeColumn
Larry Lustig
This is probably the best answer. I think I had some other data points that I did not have in my question. But this is a great answer based on my question as written.
Maestro1024