obchardon
obchardon

Reputation: 10790

Condition over a column that i just create

I can't solve a problem with a sql query.

I have a table that contains something like this:

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| A1   | B1   | C    |
| A2   | B2   | D    |
| A3   | B3   | E    |
| A3   | B3   | D    |
+------+------+------+

and a want to select only the row that have an unique Col3 if Col1 and Col2 have the same value.

So the result should be:

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| A1   | B1   | C    |
| A2   | B2   | D    |
+------+------+------+

Because row 3 and 4 have a similar Col1 and Col2 but a different Col3.

I need to put a condition over a new column that I create during the query and i can't manage how to do that.

Upvotes: 0

Views: 44

Answers (1)

user330315
user330315

Reputation:

This is a simple group by if I understand you correctly:

select col1, col2, min(col3) as col3
from the_table
group by col1, col2
having count(distinct col3) = 1

Upvotes: 2

Related Questions