Sql query with group by clause

  • 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.

  • 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!

  • 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

  • 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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply