我有下面的查询,它有一个子查询。我需要将子查询转换为一个join,并用一个单独的where子句表示日期,它将在excel spreasheet中参数化。
select me.id, me.merchant_num, me.merchant_nm,
(select count(1) from transaction_t where merchant_id = me.id and transaction_dt BETWEEN '2020-04-01' and '2020-04-30') as num_transactions
FROM merchant_t me
left outer join transaction_t tt on tt.merchant_id = me.id
where me.status = 'T'
子查询获取给定日期之间所有商户交易的计数。我已经试了所有我能想到的,但我要么得到太多的行或其他一些事情是错误的。
涉及两个表:
merchant_t
----------
id merchant_num merchant_nm status
transaction_t
--------------
id merchant_id transaction_dt
4条答案
按热度按时间mrfwxfqh1#
这应该起作用:
kgqe7b3p2#
你可以试试下面的逻辑-
6kkfgxo03#
请使用下面的查询,
您也可以使用下面的查询,
w7t8yxp54#
你的问题其实很好。几乎。你有一个不必要的
JOIN
在外部查询中:尽管你要求
left join
,此版本可能具有更好的性能(在transact_t(merhant_id, transaction_dt)
因为它避免了外部聚集。