Maestro1024
Maestro1024

Reputation: 3293

Getting unique rows/values in SQL

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

Answers (7)

Gary McGill
Gary McGill

Reputation: 27556

Use SELECT DISTINCT ?

As in:

SELECT DISTINCT value1, value2, value3 FROM table1, table2

Upvotes: 1

Larry Lustig
Larry Lustig

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

wallenborn
wallenborn

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

Maximilian Mayerl
Maximilian Mayerl

Reputation: 11367

SELECT DISTINCT *
FROM table1, table2

Upvotes: 1

Roberto Aloi
Roberto Aloi

Reputation: 31015

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

  • Unique
  • Distinct
  • Group By

Upvotes: 1

Steve Wortham
Steve Wortham

Reputation: 22260

How about this?

SELECT DISTINCT * FROM Table1, Table2

Upvotes: 0

Matt Hamsmith
Matt Hamsmith

Reputation: 4036

Select Distinct * from table1, table2

Upvotes: 1

Related Questions