maolddv
maolddv

Reputation: 97

group by oracle

I have a table as below:

id             value
-------------------------
1               1
5               1
7               1
8               4

I can't get to table as below:

id             value
-------------------------
1               1
8               4

The SQL is

select id,value from table_1 group by id_a

Upvotes: 0

Views: 151

Answers (4)

EmbodyHumility
EmbodyHumility

Reputation: 3

In order to use a group by expression, you must have one or more aggregating functions: count, min, max, sum, avg etc. These functions operate on a group of rows at a time. Now when you use an aggregate function with a none aggregated column(s) you need to use the group by clause.

The below will give you the correct answer:

select min(id) id, value from table_1 group by value

Upvotes: 0

Teja
Teja

Reputation: 13534

SELECT T.value,MIN(id) AS MIN_ID 
FROM TABLE T
GROUP BY T.value;

Upvotes: 0

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

Try this:

select min(id), id_a from table_1 group by id_a

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270617

All you have here is a simple MIN() aggregate.

SELECT MIN(id), value AS id FROM table_1 GROUP BY value

Upvotes: 2

Related Questions