SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sql query with group by clause


Sql query with group by clause

Author
Message
igovada2k
igovada2k
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
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.
Attachments
Sample_Data_Tax.xls (29 views, 17.00 KB)
Lowell
Lowell
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36298 Visits: 40269
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

John Mitchell-245523
John Mitchell-245523
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18739 Visits: 16226
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
igovada2k
igovada2k
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search