选择“顶行”并在DB2中按NEWID()排序

时间:2019-07-19 13:36:16

标签: sql-server db2 db2-400

在SQL Server中,我有此查询

SELECT TOP 10 * 
FROM xTable
ORDER BY NEWID()

基本上返回10个随机行。我也用数字将最上面的行参数化,但这不是问题

我想在DB2查询中执行此操作,因为我必须使用OpenQuery从DB2获取数据并将其存储在本地临时表中,然后从中选择前x行。

如果我可以直接在DB2中实现这一点,那将是惊人的,因为我必须获得一千多行。

2 个答案:

答案 0 :(得分:1)

这是DB2 LUW还是AS400?

尝试

SELECT *
  FROM xTable
  ORDER BY ROW_NUMBER()
  FETCH FIRST 10 ROWS ONLY;

答案 1 :(得分:1)

订购不是一个好主意,尤其是对于大桌子。
我们可以使用<table class="col-12 calendar row justify-content-center"> <colgroup> <col> <col> <col> <col> <col> <col> <col> </colgroup> <tr class="head-of-calendar"> <th class="tg-kt03">Пн</th> <th class="tg-kt03">Вт</th> <th class="tg-kt03">Ср</th> <th class="tg-kt03">Чт</th> <th class="tg-kt03">Пт</th> <th class="tg-kt03">Сб</th> <th class="tg-kt03">Вс</th> </tr> <tr> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">1</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">2</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">3</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">4</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">5</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">6</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">7</button></td> </tr> <tr> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">8</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num-mini num expired" href="#" data-toggle="modal" data-target="#exampleModal">9</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">10</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">11</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">12</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">13</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">14</button></td> </tr> <tr> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">15</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">16</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">17</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">18</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Выбирете другой день"><button class="btn calendar-num num expired" href="#" data-toggle="modal" data-target="#exampleModal">19</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="Сегодня 20 июля. День свободен!"><button class="btn calendar-num num active" href="#" data-toggle="modal" data-target="#exampleModal">20</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num" href="#" data-toggle="modal" data-target="#exampleModal">21</button></td> </tr> <tr> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">22</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">23</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">24</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">25</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">26</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">27</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">28</button></td> </tr> <tr> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">29</button></td> <td class="tg-i6c6" data-toggle="tooltip" title="День свободен!"><button class="btn calendar-num num" href="#" data-toggle="modal" data-target="#exampleModal">30</button></td> </tr> <tr> </tr> </table>-tablesample-clause的内置 Db2进行LUW 功能。

  

BERNOULLI

     

BERNOULLI采样单独考虑每一行。它包括概率为P / 100的样本中的每一行(其中P是   numeric-expression1),并排除概率为1的每一行-   P / 100,独立于其他行。因此,如果numeric-expression1   评估为值10,代表百分之十的样本,每一行   将以0.1的概率包含在内,并且以概率的形式排除   0.9。

     

系统

     

SYSTEM采样允许数据库管理器确定执行采样的最有效方式。绝大多数情况下,   应用于表名的SYSTEM采样表示   表名包含在样本中的概率为P / 100,并且   以1-P / 100的概率排除。每个页面上的所有行是   包括有资格获得样品。

示例:

subselect

用于IBM i / iSeries / OS400的DB2 中不存在此功能。在该平台上使用SELECT * FROM MYTABLE TABLESAMPLE SYSTEM (0.1) FETCH FIRST 10 ROWS ONLY; 子句。