我们可以在随机和另一个db字段上使用秩函数吗?

时间:2017-02-13 18:44:51

标签: sql sql-server database

代码:

34%cd212

我想结合随机数和名称来获得排名

2 个答案:

答案 0 :(得分:0)

我不知道您为什么要使用rank()的随机数而不是仅使用row_number(),但它是这样的:

rextester:http://rextester.com/OLKI98516

create table student(
    id int not null identity(1,1) primary key
  , name varchar(255) not null
);
insert into student values
 ('Santosh'),('Kumar'),('Reddy'),('Badugula'),('SqlZim')
,('Emma'),('Xandra'),('Naida'),('Daria'),('Colby'),('Yetta')
,('Zena'),('Deacon'),('Francis'),('Lilah'),('Risa'),('Lee')
,('Vanna'),('Molly'),('Destiny'),('Tallulah'),('Meghan')
,('Deacon'),('Francis'),('Daria'),('Colby');

select 
    name
  , RandomNumber = abs(cast(cast(newid() as varbinary) as int))
  , Name_w_RandomNumber = concat(name, '_', abs(cast(cast(newid() as varbinary) as int)))
  , rank = rank() over (order by name asc)
  , row_number = row_number() over (order by name asc)
  , rank_w_Rand = rank() over (order by name,abs(cast(cast(newid() as varbinary) as int)) asc)
from student

结果:

+----------+--------------+---------------------+------+------------+-------------+
|   name   | RandomNumber | Name_w_RandomNumber | rank | row_number | rank_w_Rand |
+----------+--------------+---------------------+------+------------+-------------+
| Badugula |   1105357025 | Badugula_1036749632 |    1 |          1 |           1 |
| Colby    |   1125329440 | Colby_1442709274    |    2 |          2 |           2 |
| Colby    |   1891932149 | Colby_1045919975    |    2 |          3 |           3 |
| Daria    |   1494409363 | Daria_112566484     |    4 |          4 |           4 |
| Daria    |    666341314 | Daria_262264162     |    4 |          5 |           5 |
| Deacon   |   1530588472 | Deacon_1783529467   |    6 |          6 |           6 |
| Deacon   |    350443065 | Deacon_1150932866   |    6 |          7 |           7 |
| Destiny  |   2007923301 | Destiny_793747374   |    8 |          8 |           8 |
| Emma     |    435476101 | Emma_659930976      |    9 |          9 |           9 |
| Francis  |   1638790395 | Francis_2132056162  |   10 |         10 |          10 |
| Francis  |    793873129 | Francis_756254272   |   10 |         11 |          11 |
| Kumar    |     20071275 | Kumar_2007808448    |   12 |         12 |          12 |
| Lee      |   2069120264 | Lee_837143565       |   13 |         13 |          13 |
| Lilah    |   1319087807 | Lilah_605243166     |   14 |         14 |          14 |
| Meghan   |    487733175 | Meghan_1884481541   |   15 |         15 |          15 |
| Molly    |   2086860257 | Molly_1914281986    |   16 |         16 |          16 |
| Naida    |    169335218 | Naida_719205571     |   17 |         17 |          17 |
| Reddy    |    528578158 | Reddy_1297094295    |   18 |         18 |          18 |
| Risa     |   1826403411 | Risa_1530611023     |   19 |         19 |          19 |
| Santosh  |    723134579 | Santosh_487617337   |   20 |         20 |          20 |
| SqlZim   |    937324776 | SqlZim_738072767    |   21 |         21 |          21 |
| Tallulah |    521881065 | Tallulah_1717653898 |   22 |         22 |          22 |
| Vanna    |   1508284361 | Vanna_1620612208    |   23 |         23 |          23 |
| Xandra   |    532483290 | Xandra_493053714    |   24 |         24 |          24 |
| Yetta    |   1735945301 | Yetta_1548495144    |   25 |         25 |          25 |
| Zena     |    311372084 | Zena_1429570716     |   26 |         26 |          26 |
+----------+--------------+---------------------+------+------------+-------------+

答案 1 :(得分:0)

这是我在评论中提到的查询,不是很漂亮但功能齐全。你在这里问的是,如果我是正确的,我仍然永远不会自己使用随机值,因为每次运行它都会得到不同的结果。

select name,
       rank() over (order by name,
           ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT))) asc) as rank
from Student

编辑:使用cte显示随机数,NEWID()保证唯一,但不确定它是否仍然在使用ABS时,你需要调查它。

with cteQry As
(    select name, ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT))) NewIdVal
    from Student
)
    select name, NewIdVal,
           rank() over (order by name, NewIdVal asc) as rank
    from cteQry