[英]How to use SELECT DISTINCT and SELECT TOP n in T-SQL
I have this query: 我有这个查询:
ALTER PROCEDURE [dbo].[MySP]
@Q Nvarchar(512)
AS BEGIN
SET @Q = REPLACE(@Q, N'%', N'');
SET @Q = N'"' + @Q + N'*' + N'"';
SELECT TOP 8
[KeywordId] as ID,
[Keyword] as lable,
[Keyword] as value
FROM [dbo].[News_Keywords]
WHERE CONTAINS ([Keyword], @Q)
ORDER BY SortOrder, len([Keyword])
END
What I'm asking for, is how can I apply DISTINCT
functionality (on column [Keyword]
) to this statement? 我要问的是如何将
DISTINCT
功能(在[Keyword]
列上)应用于此语句? It's completely confusing me! 这完全让我感到困惑! Thanks in advance.
提前致谢。
You can use group by
instead of distinct
. 您可以使用
group by
而不是distinct
。 However, you have to decide where to put SortOrder
. 但是,您必须决定将
SortOrder
放在哪里。 Here is one method: 这是一种方法:
SELECT TOP 8 [KeywordId] as ID, [Keyword] as label, [Keyword] as value
FROM [dbo].[News_Keywords]
WHERE CONTAINS ([Keyword], @Q)
GROUP BY KeywordId, KeyWord, SortOrder
ORDER BY SortOrder, len([Keyword]);
Here is another: 这是另一个:
SELECT TOP 8 [KeywordId] as ID, [Keyword] as label, [Keyword] as value
FROM [dbo].[News_Keywords]
WHERE CONTAINS ([Keyword], @Q)
GROUP BY KeywordId, KeyWord
ORDER BY MAX(SortOrder), len([Keyword]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.