1

我有这张桌子

rss_user。您会看到帐户名称、应用程序名称和应用程序类型。

带有 RSS_USER_NAME RSS_NAME RSS_TYPE

示例数据

tom@gmail.com   webapp4     webapps
tom@gmail.com   webapp6     webapps
tom@gmail.com   mswin3      windows
tom@gmail.com   mswin2      windows
sakur@gmail.com mswin2      windows
sakur@gmail.com webapp6     webapps

我希望从 rss_name 和 rss_type 的组合中获得一个随机帐户。rss_name 和 rss_type 的每个组合都应该有一个随机帐户。

当前代码。完全没用:-(

SELECT *
  FROM (  SELECT    'SYNC rss_user WITH rss_user_name = "'
             || RSS_USER_NAME
             || '" , rss_name = "'
             || RSS_NAME
             || '" , rss_type = "'
             || RSS_TYPE
             || '";'
        FROM rss_user
    ORDER BY DBMS_RANDOM.VALUE)
 WHERE ROWNUM = 1

结果:

SYNC rss_user WITH rss_user_name = "tom@gmail.com" , rss_name = "webapp4" , rss_type = "webapps";
4

2 回答 2

2

这是一种方法:

SELECT *
  FROM (SELECT    'SYNC rss_user WITH rss_user_name = "'
             || RSS_USER_NAME
             || '" , rss_name = "'
             || RSS_NAME
             || '" , rss_type = "'
             || RSS_TYPE
             || '";',
                 seqnum = row_number() over (partition by rss_name, rss_type order by dbms_random.value)
         FROM rss_user
        ) t
 WHERE seqnum = 1

也就是说,使用 row_number() 和 dbms_random.value 来选择一个随机行。

于 2012-09-04T15:51:20.300 回答
0

是关于如何逐行生成随机数的精彩讨论。一旦你了解了如何做到这一点,你就可以这样做(伪代码):

select rss_name, rss_type, max(rand) from
(select rss_name, rss_type, <random number> as rand from rss_user) a
group by rss_name, rss_type
于 2012-09-04T15:46:40.973 回答