theo
theo

Reputation: 299

SQL Move the data from different rows with same ID in same rows but different column

How to move the data from different rows with same ID in same rows but different column? For example, I have this table

tblEx

--------------
|ID|Buy |Sell|
|--+----+----|
|1 |10  |    |
|1 |    |11  |
|2 |20  |    |
|2 |    |0   |
|3 |0   |    |
|3 |    |30  |
--------------

Desired Output:

--------------
|ID|Buy |Sell|
|--+----+----|
|1 |10  |11  |
|2 |20  |0   |
|3 |0   |30  |
--------------

Upvotes: 0

Views: 119

Answers (1)

John Woo
John Woo

Reputation: 263763

Based from the given example and desired result, you can use MAX()

SELECT ID, MAX(Buy) AS Buy, MAX(Sell) AS Sell
FROM TableName
GROUP BY ID

Upvotes: 3

Related Questions