SQL Server选择查询在表

时间:2017-08-24 02:00:52

标签: sql-server

我使用以下查询从表中获取前10名公司:

Select Top 10 CompanyName 
From CompanyMaster 
Where LiveProductFlag = 1 
Order By Display_Priority asc

它正在返回这样的记录。

CompanyName
------------
First Company
Second Company
First Company
Second COmpany
Third Company
Third Company
Fourth Company
Fourth Company
Fifth Company
Fifth Company

我检查了记录,但我没有重复记录。 Select Distinct不起作用。谷歌搜索后尝试了所有可能的解决方案,没有任何成功。

感谢。

1 个答案:

答案 0 :(得分:0)

您可以按如下方式对群组字段查询运行简单计数,以便仔细检查您的假设。

Select 
    CompanyName,
    DuplicateCount=COUNT(*)
From 
    CompanyMaster 
Where 
    LiveProductFlag = 1 
GROUP BY
     CompanyName
HAVING
    COUNT(*) > 1
ORDER BY
    COUNT(*) DESC