Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sql query with group by clause Expand / Collapse
Author
Message
Posted Tuesday, January 15, 2008 12:01 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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.


  Post Attachments 
Sample_Data_Tax.xls (9 views, 17.00 KB)
Post #443174
Posted Thursday, January 17, 2008 8:38 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 12,749, Visits: 31,114
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
Post #444356
Posted Thursday, January 17, 2008 8:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:58 AM
Points: 5,077, Visits: 8,919
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
Post #444363
Posted Thursday, January 24, 2008 4:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #447254
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse