有趣的SQL查询随机

时间:2012-02-22 18:58:17

标签: sql sql-server tsql

我有一个包含1000行的表,其格式如下

id d1  date                        Type
   1  N   2012-03-09 00:00:00.000     C
   2  No  2011-09-26 00:00:00.000     C
   3  N   2012-01-26 00:00:00.000     R
   4  N   2012-03-07 00:00:00.000     R
   5  yes 2012-02-23 00:00:00.000     C
   6  N   2012-03-09 00:00:00.000     C
   7  No  2011-09-26 00:00:00.000     C
   8  N   2012-01-26 00:00:00.000     R
   9  N   2012-03-07 00:00:00.000     R
   10  yes 2012-02-23 00:00:00.000     C

我想要像下面那样放

id d1  date                        Type
   1  N   2012-03-09 00:00:00.000     C
   3  No  2011-09-26 00:00:00.000     R
   2  N   2012-01-26 00:00:00.000     C
   4  N   2012-03-07 00:00:00.000     R
   5  yes 2012-02-23 00:00:00.000     C
   8  N   2012-03-09 00:00:00.000     R
   7  No  2011-09-26 00:00:00.000     C
   9  N   2012-01-26 00:00:00.000     R
   6  N   2012-03-07 00:00:00.000     c
   10  yes 2012-02-23 00:00:00.000     R

我需要显示10个随机行,该顺序应该基于TYPE字段。和TYPE FIELD应该像

c
r
c
r
c
r

图案。

4 个答案:

答案 0 :(得分:4)

我使用一些RAND()调用解决了这个问题。我试图制作计算列,但插入的都是相同的值。该值在调用之间是随机的,但在查询中是相同的。每次对RAND()插入一个不同的调用,但我不得不为每个值重复一个案例逻辑,这不是很理想。以下是我解决这个问题的方法:

DECLARE @RandomizedTypes TABLE
(
    TempId FLOAT
    , d1 VARCHAR(3)
    , [Date] DATETIME
)

INSERT @RandomizedTypes
(
    TempId
    , d1
    , [Date]
)
VALUES
    (RAND(), 'N', '2012-03-09 00:00:00.000')
    , (RAND(), 'No', '2011-09-26 00:00:00.000')
    , (RAND(), 'N', '2012-01-26 00:00:00.000')
    , (RAND(), 'N', '2012-03-07 00:00:00.000')
    , (RAND(), 'yes', '2012-02-23 00:00:00.000')
    , (RAND(), 'N', '2012-03-09 00:00:00.000')
    , (RAND(), 'No', '2011-09-26 00:00:00.000')
    , (RAND(), 'N', '2012-01-26 00:00:00.000')
    , (RAND(), 'N', '2012-03-07 00:00:00.000')
    , (RAND(), 'yes', '2012-02-23 00:00:00.000')

DECLARE @FinalRandomizedTable TABLE
(
    id INT IDENTITY(1, 1)
    , d1 VARCHAR(3)
    , [Date] DATETIME
    , [Type] AS CASE WHEN (id % 2) = 0 THEN 'R' ELSE 'C' END
)

INSERT @FinalRandomizedTable
(
    d1
    , [Date]
)
SELECT
    d1
    , [Date]
FROM @RandomizedTypes
ORDER BY TempId

SELECT * FROM @FinalRandomizedTable

编辑:修改为替代' R'和' C'根据更新的问题行。

答案 1 :(得分:1)

这是一种方式:

SELECT t.id
       ,t.d1
       ,t.[date]
       ,t.[Type]
  FROM
    (
    -- Get 10 random rows
    SELECT TOP 10 id
           ,d1
           ,[date]
           ,[Type]
      FROM [table]
     ORDER BY NEWID() -- use NewID() to randomize result set
    ) t
     ORDER BY ROW_NUMBER() OVER (PARTITION BY t.[Type] ORDER BY t.id) ASC
     ,t.[Type] ASC

答案 2 :(得分:0)

我认为这就是你想要的。

SELECT id,d1,date,Type
FROM (
select ((RANK() OVER ( ORDER BY randomRank)) * 2 + 1) rank, id,d1,date,Type
FROM (SELECT top 10 NEWID() randomRank,id,d1,date,Type FROM YourTable  WHERE Type = 'C' ORDER BY NEWID()) a
UNION 
select ((RANK() OVER ( ORDER BY randomRank)) * 2 ) rank, id,d1,date,Type
FROM (SELECT top 10 NEWID() randomRank,id,d1,date,Type FROM YourTable  WHERE Type = 'R' ORDER BY NEWID()) a
)b order by rank

答案 3 :(得分:-2)

;with abcd
as
(
    select id, d1, date, types,
           ROW_NUMBER() over(partition by types order by types asc) as number 
    from sampledata 
)

select * from abcd order by number,types