Problem in Distinct Query

  • deba_20032004

    Default port

    Points: 1476

    Dear Sir,

    I am in great trouble, I have a table which contain 100 fields. I want to distinct on first two fields and with full information of 100 fields.

    How to solve the problem, Plz. help me.

    Thanking You

    Debasis Bag

  • John Mitchell-245523

    SSC Guru

    Points: 148769

    So you only want one row returned for each unique combination of those two columns? How are you going to decide which row that will be when there's more than one in the table? Please provide table DDL in the form of a CREATE TABLE statement, sample data in the form of INSERT statements, and expected results.


  • Grant Fritchey

    SSC Guru

    Points: 396620

    Without the structure, this is just a guess, but couldn't you do a subselect to get the unique set of two columns and then join that to the rest of the table...hmmm.... No, that might not work if there are duplicate versions of those values. Ah, how about using row_number then order by the values and get the PK that matches THEN join that out to the rest of the table. That would work.

    The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
    Theodore Roosevelt

    The Scary DBA
    Author of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd Edition
    Product Evangelist for Red Gate Software

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

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