|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 01, 2008 11:56 AM
Points: 8,
Visits: 26
|
|
I have Sql Server 2000 database. I want the records with maximum effective date for each zip, and state combination to be put in another table.
For each Zip and state combination, I want the records for all cities, but no duplicate cities.
If cities are duplicate, I want the one with CntyDef = 'Y'. So out of the data (attached excel), I want the rows highlighted in yellow. There are comments in the excel sheet on rows as to how the data is required.
What kind of query should i use? I'm trying to use group by clause and subquery but not successful. Please help.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:55 AM
Points: 11,605,
Visits: 27,643
|
|
i put to gether the table and data, but didn't have time to continue; this might help in the analysis:
create table #SpreadSheet( Zip varchar(9), City varchar(60), County varchar(60), State varchar(2), CntDef char(1), Ddef char(1), CombSTax decimal(10,4), CombUTax decimal(10,4), EffectiveDate datetime )
INSERT INTO #SpreadSheet VALUES ('90001','FIRESTONE PARK ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('90001','FIRESTONE PK ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('90001','LOS ANGELES ','LOS ANGELES ','CA',' ','Y',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('90001','FIRESTONE PARK ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2005') INSERT INTO #SpreadSheet VALUES ('90001','FIRESTONE PK ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2005') INSERT INTO #SpreadSheet VALUES ('90001','LOS ANGELES ','LOS ANGELES ','CA',' ','Y',0.0825,0.0825,'1/1/2005') INSERT INTO #SpreadSheet VALUES ('90002','AUGUST F. HAW ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('90002','LOS ANGELES ','LOS ANGELES ','CA',' ','Y',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('90002','WATTS ','LOS ANGELES ','CA',' ',' ',0.0825,0.0825,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('96161','NORTHSTAR ','NEVADA ','CA',' ',' ',0.07375,0.07375,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('96161','TRUCKEE ','NEVADA ','CA','Y','Y',0.07875,0.07875,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('96161','TRUCKEE ','PLACER ','CA',' ',' ',0.0725,0.0725,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('96162','TRUCKEE ','NEVADA ','CA',' ',' ',0.07875,0.07875,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('95526','BRIDGEVILLE ','HUMBOLDT ','CA',' ','Y',0.0725,0.0725,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('95526','RUTH ','HUMBOLDT ','CA',' ',' ',0.0725,0.0725,'1/1/2002') INSERT INTO #SpreadSheet VALUES ('95526','RUTH ','TRINITY ','CA','Y',' ',0.0725,0.0725,'1/1/2002')
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 9:56 AM
Points: 4,418,
Visits: 7,156
|
|
Can you show us what you've tried, and then maybe we can point out where you're going wrong? Otherwise it's too much like we're doing all the work for you.
John
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 01, 2008 11:56 AM
Points: 8,
Visits: 26
|
|
I tried to make this query below and it worked. Thanks for your help. Select * FROM TestImportTax T INNER JOIN (SELECT Zip, City, State, EffDate = Max(EffDate), CntyDef = max(CntyDef) FROM TestImportTax GROUP BY Zip, City, State ) T1 ON T.Zip = T1.Zip AND T.City = T1.City AND T.State = T1.State AND T.EffDate = T1.EffDate AND T.CntyDef = T1.CntyDef
|
|
|
|