1

我有下表

ind_id      name                 value       date
----------- -------------------- ----------- ----------
1           a                    10          2010-01-01
1           a                    20          2010-01-02
1           a                    30          2010-01-03
2           b                    40          2010-01-01
2           b                    50          2010-01-02
2           b                    60          2010-01-03
2           b                    70          2010-01-04
3           c                    80          2010-01-01
3           c                    90          2010-01-02
3           c                    100         2010-01-03
3           c                    110         2010-01-04
3           c                    120         2010-01-05
4           d                    130         2010-01-05

如何选择具有唯一ind_id属性的行?不管每一行肯定是什么ind_id。我需要一个结果集中的 4 行。我正在使用 SQL Server 2008。谢谢

4

1 回答 1

4

为此,您要使用 windows 功能:

select ind_id, name, value, date
from (select t.*, row_number() over (partition by ind_id order by (select NULL)) as seqnum
      from t
     ) t
where seqnum = 1;

如果您只想要出现一次的那些,请使用:

select ind_id, name, value, date
from (select t.*, count(*) over (partition by ind_id) as NumInds
      from t
     ) t
where numInds = 1
于 2013-04-01T13:41:53.427 回答