Select and ID Column

  • I had posted a question last week about about a getting rid of duplicates in a select statement that was answered thanks to people on this forum. After working on it I ran into a problem this weekend.

    I have the following SQL statment that returns the correct number of rows

     

    select

    Distinct CustomerName,CustomerNumber,SalesmanName,Min(ItemDescription) AS ItemDescription,OrigionalDateofService,InsuranceCarrier, Diag1,Diag2,Diag3,Diag4 from O2

    Group By

    CustomerName,

    CustomerNumber,

    SalesmanName,

    OrigionalDateofService,

    InsuranceCarrier,

    Diag1,

    Diag2,

    Diag3,

    Diag4

    The problem that I run into is that my O2 table also has and ID column, an auto Identity field, that must be included within the result set for development reasons.

    When I add the ID to the select statement it returns all the rows of the column. What would be the correct way to include the ID field and get the right set of data.

  • Well one thing is you'd have to determine what "id" you want to return. Do you just return one of them for each distinct set you have or ..do you need to return each one ?


    Mathew J Kulangara
    sqladventures.blogspot.com

  • I only need to return the one for the distinct combination.

  • If I'm understanding you...

    select Distinct [ID],CustomerName,CustomerNumber,SalesmanName,Min(ItemDescription) AS ItemDescription,OrigionalDateofService,InsuranceCarrier, Diag1,Diag2,Diag3,Diag4 from O2

    Group By

    [ID],

    CustomerName,

    CustomerNumber,

    SalesmanName,

    OrigionalDateofService,

    InsuranceCarrier,

    Diag1,

    Diag2,

    Diag3,

    Diag4

    By the way if {id] is trully an identity column ..you shouldn't need distinct at all.

    HTH


    Mathew J Kulangara
    sqladventures.blogspot.com

  • Try the following:

    select Distinct CustomerName,CustomerNumber,SalesmanName,Min(ItemDescription) AS ItemDescription,OrigionalDateofService,InsuranceCarrier, Diag1,Diag2,Diag3,Diag4,

    min(ID) as ID

    from O2

    Group By

    CustomerName,

    CustomerNumber,

    SalesmanName,

    OrigionalDateofService,

    InsuranceCarrier,

    Diag1,

    Diag2,

    Diag3,

    Diag4

    This will still allow you to get only the distinct combinations of your data w/o having the ID field introduce artificial uniqueness.

    Have fun!

    Steve G.

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

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