sql - Group by with null values -
sql - Group by with null values -
trying grouping result bellow:
+-------------+------+ | id | coln1 | coln2 | +-------------+------+ | 10 | | null | | 10 | null | x | | 15 | b | null | | 15 | null | y | | 20 | c | null | | 20 | null | z | +--------------+-----+ with next select:
select * ( select db1.dbo.table1.id id, db1.dbo.table1.coln1 coln1, null coln2 db1.dbo.table1 union select db2.dbo.table2.id id, null coln1, db2.dbo.table2.coln2 coln2 db2.dbo.table2 ) grouping id, coln1, coln2 order id asc expecting this:
+-------------+------+ | id | coln1 | coln2 | +-------------+------+ | 10 | | x | | 15 | b | y | | 20 | c | z | +--------------+-----+ with no luck. tried utilize having no luck neither. info on this.
an aggregate function should help you. aggregate function eliminate null values.try this
select id, max(coln1), max(coln2) tablename grouping id update query this..
select id, max(coln1), max(coln2) (select db1.dbo.table1.id id, db1.dbo.table1.coln1 coln1, null coln2 db1.dbo.table1 union select db2.dbo.table2.id id, null coln1, db2.dbo.table2.coln2 coln2 db2.dbo.table2) grouping id order id asc sql sql-server
Comments
Post a Comment