Reputation: 3293
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.
Upvotes: 0
Views: 513
Reputation: 27556
Use SELECT DISTINCT
?
As in:
SELECT DISTINCT value1, value2, value3 FROM table1, table2
Upvotes: 1
Reputation: 51008
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
Upvotes: 3
Reputation: 4273
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;
Upvotes: 0
Reputation: 31015
You might want to have a look to the SQL reference for:
Upvotes: 1