0

How can I combine 2 rows into 1 with sqlite-android.

There are 2 rows in my sqlite table.

Basically, parent does not have any 'null' value. However, child may have 'null'.

child's 'null' will be changed to parent's 'non-null' value.

(example)
title     item1     item2     item3
------------------------------------
parent    1         2         3
child     null      20        null

If I combine above 2 rows,

(combine above table)
title     item1     item2     item3
------------------------------------
child     1         20        3

How can I do this?

david
  • 53
  • 1
  • 8

1 Answers1

0

To combine two rows into one output row, use a self join; to replace NULL values, use ifnull():

SELECT c.title,
       ifnull(c.item1, p.item1),
       ifnull(c.item2, p.item2),
       ifnull(c.item3, p.item3)
FROM MyTable AS p
JOIN MyTable AS c ON p.title = 'parent'
                 AND c.title = 'child';
CL.
  • 173,858
  • 17
  • 217
  • 259