sql - Can I modify below GroupBy clause into better one -
consider following cases
case 1: tbl_a --------------- | cola | colb | --------------- expected o/p: 1 0 | 1 | 0 | --------------- case 2: tbl_a --------------- | cola | colb | --------------- expected o/p: 1 1 | 1 | 1 | --------------- case 3: tbl_a --------------- | cola | colb | --------------- expected o/p: 1 1 | 1 | 0 | | 1 | 1 | --------------- case 4: tbl_a --------------- | cola | colb | --------------- expected o/p: null null | null | null | ---------------
the query simple, if there record cola = 1 , colb = 1 return it, if no such record exists return existing record cola = 1. have tried various ways. came groupby clause there simple way it.
if use cola = 1 , colb = 1 fails case 1 returns no rows.
select cola, max(colb) group (cola)
is valid query? appreciated.
please try following. provides desired results posted cases.
select top 1 cola, colb tbl_a cola = 1 or cola null order cola desc, colb desc;
Comments
Post a Comment