T-SQL returning 1 record

  • I have this table with:

    columna    columnb

    26806       26804

    26806       42485

    10701       10401

    10701       10402

    10701       10403

    10701       10404

    How should I build the query so that it will return just 1 record for 26806 and 10701?

    Thanks for helping me.

  • SEELCT ColumnA,MIN(ColumnB) OR MAX(ColumnB)

    FROM tblXYZ

    GROUP BY ColumnA

    Ram

  • Thanks Sriram. 

    What if I have several more columns after columnb?  I tried your query when I have several columns after columnb and the query didn't work.

     

  • Here's one way:

    SELECT top 2 columna, columnb , DENSE_RANK() OVER (PARTITION BY columna ORDER BY columnb) AS DENSE_RANK

    FROM yourTable

    ORDER BY dense_rank

    ******************
    Dinakar Nethi
    Life is short. Enjoy it.
    ******************

  • Try this out (If you have more columns)

    SELECT A.*

    FROM tblXYZ AS A

     INNER JOIN  (

       SELECT  ColumnA,MIN(ColumnB) AS ColumnB

       FROM  tblXYZ

       GROUP BY ColumnA

      &nbsp AS B

      ON  A.ColumnA = B.ColumnA

      AND A.ColumnB = B.ColumnB

    Ram

     

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

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