根据其他列值过滤掉一个列值

时间:2013-12-20 06:57:20

标签: sql-server sql-server-2008-r2 unique distinct

暂时陷入困境。我给你和下面的例子。

我有这张桌子

ID       | Dip  | Azi   |
SV12360  |-45   | 229.3 |
SV12360  |-45.1 | 228.3 |
SV12360  |-44.8 | 223.3 |
SV12359  |-39   | 249.3 |
SV12359  |-39.4 | 279.3 |
SV12357  |-55   | 290.3 |
SV12357  |-57   | 210.3 |
SV12318  |-47   | 212.3 |
SV12318  |-47.2 | 241.3 |
SV12317  |-41   | 289.3 |
SV12317  |-40   | 211.3 |
------------------------- etc.

我想创建一个查询,只为每个ID提供第一个值。例如,在这种情况下,我希望看到具有第一个Dip和Azi值的不同ID。

ID        Dip   Azi
SV12360 | -45 | 229.3
SV12359 | -39 | 249.3
SV12357 | -55 | 290.3

等。这些表包含大约19000行,我不想手动复制粘贴每个ID的值。

2 个答案:

答案 0 :(得分:1)

SELECT  t2.*
FROM    (
        SELECT  DISTINCT ID
        FROM    <yourTable>
        ) t1
CROSS APPLY
        (
        SELECT  TOP 1 *
        FROM    <yourTable> t
        WHERE   t.ID = t1.id
        --ORDER BY <column> --you can specify which record is first            
        ) t2

答案 1 :(得分:0)

try below



WITH ABC AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY id ORDER BY ID)'rank' FROM TBL)

    SELECT * FROM ABC 
    WHERE rank= 1

OR

select * from(
(SELECT *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID)'rank'
             FROM TBL)
             )tmp where rank=1