Author |
Topic |
tocroi72
Yak Posting Veteran
89 Posts |
Posted - 2006-11-07 : 15:26:04
|
Hello all,I have a table like this:create table #address (entityCode varchar(100),addresstype char(1),address1 varchar(50),address2 varchar(50),city varchar(50),enddate datetime)insert into #addressselect '111111','M','1233 chate dr',null,'gardena',getdate()-30unionselect '111111','F','123 chate dr',null,'gardena',getdate()- 20unionselect '111111','D','122 chate dr',null,'gardena',getdate() - 10unionselect '111111','D','122 rose dr',null,'gardena',Nullunionselect '22222','F','100 rose dr',null,'gardena',getdate()- 20unionselect '22222','D','108 chate dr',null,'gardena',getdate() - 10unionselect '22222','D','145 chate dr',null,'gardena',getdate() -2unionselect '22222','D','190 rose dr',null,'gardena',Nullunionselect '34536','D','100 rose dr',null,'gardena',getdate()- 20unionselect '34536','D','108 chate dr',null,'gardena',NULL----------------------i'd like to have a select result as below:111111 M 1233 chate dr NULL gardena 2006-10-08 14:15:13.44322222 F 100 rose dr NULL gardena 2006-10-18 14:15:13.44334536 D 108 chate dr NULL gardena NULL--------------------------what i 'd like to accomplish is if addresstype = 'M' then AddressMelseif addresstype = 'F' then addressFelse addressD.and also, i need to select the Max (enddate) if there're 2 records of one type of address, or if enddate is null then that the record i want. In order to get the result i wanted , i have to create 3 temp tables where addresstype = 'F' and 'M' and 'D' and do the select max(...) , i wonder if there're a shorter way to do it? .Thanks for all your idea. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-07 : 15:38:49
|
[code]-- prepare test datacreate table #address(entityCode varchar(100),addresstype char(1),address1 varchar(50),address2 varchar(50),city varchar(50),enddate datetime)insert into #addressselect '111111','M','1233 chate dr',null,'gardena',getdate()-30unionselect '111111','F','123 chate dr',null,'gardena',getdate()- 20unionselect '111111','D','122 chate dr',null,'gardena',getdate() - 10unionselect '111111','D','122 rose dr',null,'gardena',Nullunionselect '22222','F','100 rose dr',null,'gardena',getdate()- 20unionselect '22222','D','108 chate dr',null,'gardena',getdate() - 10unionselect '22222','D','145 chate dr',null,'gardena',getdate() -2unionselect '22222','D','190 rose dr',null,'gardena',Nullunionselect '34536','D','100 rose dr',null,'gardena',getdate()- 20unionselect '34536','D','108 chate dr',null,'gardena',NULL-- stage the datadeclare @address table (rowid int identity(1, 1), entityCode varchar(100),addresstype char(1),address1 varchar(50),address2 varchar(50),city varchar(50),enddate datetime)insert @address ( entityCode, addresstype, address1, address2, city, enddate )SELECT entityCode, addresstype, address1, address2, city, enddate FROM #AddressORDER BY EntityCode, CASE WHEN AddressType = 'F' THEN 0 WHEN AddressType = 'M' THEN 1 ELSE 2 END, CASE WHEN EndDate IS NULL THEN 0 ELSE 1 END, EndDate-- do the workSELECT entityCode, addresstype, address1, address2, city, enddate FROM @AddressWHERE rowid in (select min(rowid) from @address group by entitycode)DROP TABLE #address[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|