I have a table with only a full column in each row And the rest of the columns are null as follows:
+--------------------------------------------------------------------------------+ | f_id | cloumn1 | cloumn2 | cloumn3 | cloumn4 | ---------------------------------------------------------------------------------- | 1 | data | NULL | NULL | NULL | ---------------------------------------------------------------------------------- | 1 | NULL | data | NULL | NULL | ---------------------------------------------------------------------------------- | 1 | NULL | NULL | data | NULL | ---------------------------------------------------------------------------------- | 1 | NULL | NULL | NULL | data | +--------------------------------------------------------------------------------+ | 2 | data1 | NULL | NULL | NULL | ---------------------------------------------------------------------------------- | 2 | NULL | data1 | NULL | NULL | ---------------------------------------------------------------------------------- | 2 | NULL | NULL | data1 | NULL | ---------------------------------------------------------------------------------- | 2 | NULL | NULL | NULL | data1 | +--------------------------------------------------------------------------------+
I need to Receive all data in one row
+----------------------------------------------------------------------------------------+ | f_id | cloumn1 | cloumn2 | cloumn3 | cloumn4 | ------------------------------------------------------------------------------------------ | 1 | data | data | data | data | ------------------------------------------------------------------------------------------ | 2 | data1 | data1 | data1 | data1 | +----------------------------------------------------------------------------------------+
Do you have a good way of writing this query?
Advertisement
Answer
You may use Group by
and Max
.
SELECT f_id, MAX(cloumn1) AS cloumn1, MAX(cloumn2) AS cloumn2, MAX(cloumn3) AS cloumn3, MAX(cloumn4) AS cloumn4 FROM table GROUP BY f_id