我可以用ksql编写子查询吗?

kupeojn6  于 2021-06-06  发布在  Kafka
关注(0)|答案(3)|浏览(367)

我是新来的 ksql 一直在使用 mysql 最长的时间。我想知道是否还有子查询在里面 KSQL ?
此查询工作正常:

SELECT a.executedate, count(a.pno), sum(a.amount) FROM
tb3_withdraw_record_summary a WHERE a.status='3' GROUP BY
a.executedate;

然而,此查询返回错误消息:

SELECT a.executedate, count(a.pno), sum(a.amount), (SELECT COUNT(b.pno)
FROM tb3_withdraw_record_summary b WHERE b.status='5' AND
b.executedate = a.executedate) FROM tb3_withdraw_record_summary a
WHERE a.status='3' GROUP BY a.executedate

'准备语句失败:'b'不是有效的流/表名称或别名。原因:“b”不是有效的流/表名称或别名
不管怎样,我能让它工作吗?谢谢!

ws51t4hk

ws51t4hk1#

我不熟悉ksql,但也许这正是您想要的:

SELECT wrs.executedate,
       SUM(CASE WHEN wrs.status IN ('3') THEN 1 ELSE 0 END), 
       SUM(CASE WHEN wrs.status IN ('3') THEN amount ELSE 0 END), 
       SUM(CASE WHEN wrs.status IN ('5') THEN 1 ELSE 0 END) 
FROM tb3_withdraw_record_summary wrs
WHERE wrs.status IN ('3', '5')
GROUP BY wrs.executedate;
czq61nw1

czq61nw12#

ksql没有实现子查询功能。
https://github.com/confluentinc/ksql/issues/745

fhg3lkii

fhg3lkii3#

ksql当前不支持嵌套查询功能,但可以通过以下方式实现-

1) CREATE STREAM B AS SELECT COUNT(b.pno)
FROM tb3_withdraw_record_summary b WHERE b.status='5';
2) SELECT a.executedate, count(a.pno), sum(a.amount) FROM tb3_withdraw_record_summary a JOIN B within 5 hours ON b.executedate = a.executedate WHERE a.status='3' GROUP BY a.executedate

请记住,连接的含义与关系数据库世界截然不同,这里的数据是通过多个bucket中的键进行分区的,从概念上讲,它是一个“共定位”连接。更多关于时间窗口的细节。
希望能有帮助。

相关问题