从表中选择具有不同值的所有列

时间:2014-01-29 06:11:34

标签: sql sql-server tsql

我有一个包含这些列的表 ID,NAME,JOB 我想要的是选择表格中每个不同工作的一条记录

来自此表

ID NAME JOB
1  Juan  Janitor
2  Jun   Waiter
3  Jani  Janitor
4  Jeni  Bartender

这样的事情

ID NAME JOB
1  Juan Janitor
2  Jun  Waiter
4  Jeni Bartender

使用distinct将允许我选择一个不同的列,但我想选择表中的每一列,任何人都知道如何?

4 个答案:

答案 0 :(得分:3)

你可以试试这个

SELECT ID, NAME,JOB FROM
(
SELECT ID, NAME,JOB,Row_Number() Over (Partition BY NAME Order By ID) AS RN FROM `table1`
) AS T

WHERE RN = 1

答案 1 :(得分:1)

SELECT MIN(ID), NAME, JOB FROM `table`
Group by NAME, JOB

答案 2 :(得分:0)

- 仅获取Rnk = 1记录以获得所需的输出

SELECT ID,Rank() Over(Partition By Job Order By ID) As Rnk, Name,JOB
From EMP

答案 3 :(得分:0)

SELECT ID, NAME,JOB FROM
(
    SELECT ID,Rank() Over(Partition By Job Order By ID) As jobRank, Name,JOB from #abhi
) AS TTable

WHERE jobRank = 1