我只是想知道是否可以得到三个随机结果,然后按字母顺序按另一列排序。
示例:
SELECT TOP(3)[Name]
FROM Table
ORDER BY NEWID()
¿ORDER BY [Surname]?
谢谢。
答案 0 :(得分:2)
不确定100%理解问题,但是再加上ORDER BY
,它将对所有差异ID进行排序。
我做了CTE来达到这个结果。
WITH CTE AS
(
SELECT TOP(3)[Name]
FROM Table
ORDER BY NEWID()
)
SELECT *
FROM CTE
ORDER BY [Surname]
答案 1 :(得分:1)
有一个派生表(子查询),您可以在其中选择3个随机行。 ORDER BY
其结果。
select [Name]
from
(
SELECT TOP(3) [Name], [Surname]
FROM Table
ORDER BY NEWID()
) dt
ORDER BY [Surname]
答案 2 :(得分:0)
您可以尝试一下。
SELECT T.*
FROM
(
SELECT TOP(3)[Name], [Surname]
FROM Table
ORDER BY NEWID()
) AS T
ORDER BY [Surname];
答案 3 :(得分:0)
您可以尝试--
declare @name table
(Name varchar(20), SurName varchar(20))
insert into @name
values
('mukesh', 'arora'),
('amit', 'kumar'),
('Vijay', 'gupta'),
('jai', 'poddar'),
('vishal', 'sharma')
select Name from
(
SELECT TOP(3)[Name] , [Surname]
FROM @name
ORDER BY NEWID()
) a
ORDER BY [Surname]