我有这样的表设置
CompanyName FileName
ABC abc.gif
ABD logo.gif
ZBC zbc.gif
ZVC zbc.gif
我正在寻找一个选择不同文件名的查询,并按CompanyName对结果进行排序。
这不起作用:
SELECT distinct FileName
FROM table
where
CompanyName in (
select CompanyName from Table GROUP BY CompanyName
)
答案 0 :(得分:3)
您只能按查询返回的列进行排序,因此如果您想按此排序,则必须将公司名称作为结果的一部分。但是,由于您似乎在清理数据,我建议如下:
; WITH c AS (SELECT CompanyName, FileName, Rn = ROW_NUMBER() OVER (PARTITION BY FileName ORDER BY CompanyName) FROM table1)
SELECT FileName, CompanyName
FROM c
WHERE RN = 1
ORDER BY CompanyName