selects columns from a table with the distinct operator

SPWGUT 121 Reputation points
2022-07-05T06:46:45.213+00:00

Hi,team!

I want to selects multiple columns from a table with the distinct operator on one column only.
I am trying to select all the Type with their corresponding tb_ID.

I have tried:
Select Distinct(type), tb_ID FROM tbl
-Returns all results, not just distinct

Select Type, tb_ID FROM tbl Group By Type--wrong

Could you help me!Thank you for your time!

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,688 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bert Zhou-msft 3,436 Reputation points
    2022-07-05T08:09:10.327+00:00

    Hi,@SPWGUT

    If I understand you correctly, I think you should use max() or min() aggregation function for tb_ID, try this solution:

    select Type,Tb_ID  from tbl   
    where tb_ID in (  
          Select max(tbl_ID) FROM tbl   
          group by Type  
    )  
    

    Bert Zhou

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 45,811 Reputation points
    2022-07-05T06:56:51.88+00:00

    distinct operator on one column only.

    That's not possible and don't make sense; which result would you expect here.

    And at all, that's to less on information.
    Please post table design as DDL, some sample data as DML statement and the expected result.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.