From this table:
Select * into #tmp from (
select 'a' A, 'b' B, NULL C union all
select 'aa' A, 'ab' B, 1 C union all
select 'aaa' A, 'bbb' B, 2 C ) x
I'd like to get this result:
A B Val
a b 1
aa ab 1
aaa bbb 2
That is, take the non-null min value and replace the NULL.
I suppose I could join the table to a filtered version of itself where no nulls appear. But that seems overkill. I thought this might be able to be done in the MIN Aggregate clause itself.
Any ideas would be helpful, thanks!
declare @null int
select @null = MIN(c) from #tmp
select A, B, ISNULL(c,@null) as val1 from #tmp
or
select A, B, ISNULL(c,(select MIN(c) from #tmp)) as val1 from #tmp
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments