Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Does the Coalesce function solve this?

Author  Topic 

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2001-07-18 : 10:05:27
How about this one...

I have two tables with customer IDs and flags. I may have the same customer in both tables - the flag I want to end up with should be the non null value (there cannot be different non null values across the tables.

I have done this using the following:-

create table test1 (urn varchar(10), flag1 varchar(10),flag2 varchar(10))
create table test2 (urn varchar(10), flag1 varchar(10),flag2 varchar(10))

insert into test1 values (1,'L',null)
insert into test1 values (2,'N',null)
insert into test2 values (1,null,'S')
insert into test2 values (3,'N',null)

select * into test3 from test1
union
select * from test2

select URN,flag1=max(flag1),flag2=max(flag2) into test4 from test3 group by urn having count(*)>1
delete from test3 where exists (select * from test4 where test3.urn=test4.urn)

select * into test5 from test3
union
select * from test4



This is the result (contents of test5):-

urn flag1 flag2
=== ===== =====
1 L S
2 N NULL
3 N NULL

Can I do this somehow using coalesce in a single select?

PS. I am trying to avoid updates as I am working with 3 million + records



Edited by - davidpardoe on 07/18/2001 11:16:59

Edited by - davidpardoe on 07/18/2001 11:17:49
   

- Advertisement -