SQL:如何为以下内容编写查询

时间:2013-11-04 04:45:51

标签: sql-server-2005

我有一张表格如下:

FA_Name| SP_Name | ALIAS_Name
------------------------------
FA1    |   SP1    | a1
FA1    |   SP1    | a2
FA1    |   SP2    | a3
FA1    |   SP2    | a4
FA1    |   SP3    | a5
FA1    |   SP3    | a6

我想只检索给定FA_Name的每个不同“SP_Name”的第一个ALIAS_Name记录。这意味着,我必须按如下方式获取记录:

FA1 |   SP1   | a1
FA1 |   SP2   | a3
FA1 |   SP3   | a5

请帮我写同样的SQl查询。感谢。

1 个答案:

答案 0 :(得分:0)

我得到了同样的回答,如下:

WITH duplicate AS 
(
SELECT fu_area, sp_name, analysis_name, ROW_NUMBER() OVER(PARTITION BY fu_area, sp_name 
ORDER BY fu_area, sp_name, analysis_name) AS _dupe_num 
FROM temp_rrr_test_111 WHERE 1=1
)
select * FROM duplicate WHERE _dupe_num = 1