gpt4 book ai didi

sql - 在 NZSQL 中使用 limit with union all 时出错

转载 作者:行者123 更新时间:2023-12-02 01:04:22 25 4
gpt4 key购买 nike

我试图在一周内对随机条目进行随机抽样。为了确保工作日和周末条目的平衡,我草拟了 2 个单独的 sql 语句,如下所示:

select * FROM admin.acct_activity
where RANDOM() <=1
--weekday
and extract(day from page_hit_ts) Between 6 and 10
limit 500
UNION all

SELECT *
FROM admin.acct_activity
where RANDOM() <=1
--weekend
and extract(day from page_hit_ts) Between 11 and 12
limit 200
/* to-do
1. limit results for each query
2. order query results by date*/

错误信息是:

"all" (at char 121) expecting a keyword (State:42000, Native Code: 1B)

这是在 Netezza SQL (NZSQL) 中

第一个 select 语句中的 limit 语句似乎是导致错误的原因。有什么建议吗?

最佳答案

我想你可以用括号来做到这一点:

(select *
FROM admin.acct_activity
where RANDOM() <=1 and extract(day from page_hit_ts) Between 6 and 10
limit 500
)
UNION all
(SELECT *
FROM admin.acct_activity
where RANDOM() <=1 and extract(day from page_hit_ts) Between 11 and 12
limit 200
)

如果没有,您绝对可以使用子查询来完成:

select t.*
from ((select *
FROM admin.acct_activity
where RANDOM() <=1 and extract(day from page_hit_ts) Between 6 and 10
limit 500
)
UNION all
(SELECT *
FROM admin.acct_activity
where RANDOM() <=1 and extract(day from page_hit_ts) Between 11 and 12
limit 200
)
) t

关于sql - 在 NZSQL 中使用 limit with union all 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24288141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com