gpt4 book ai didi

sql - 简化嵌套查询

转载 作者:行者123 更新时间:2023-12-04 20:56:41 24 4
gpt4 key购买 nike

我想获取在给定日期添加和删除的记录(基于 id_external ),所以我使用了下面的查询,它给了我预期的结果,但它花费了很多时间并且 LOGICALREAD 太高了,...有人可以简化它吗

SELECT ar.*
FROM t_row_data ar
WHERE ar.id_instance IS NULL
AND ar.id_category IS NULL
AND ar.source_name ='SomeSource'
AND ar.eco_date IN (date '2017-12-22', date '2017-12-21')
AND ar.active = 'Y'
AND ar.id_external IN
(SELECT ar.id_external
FROM t_row_data ar
WHERE ar.id_instance IS NULL
AND ar.id_category IS NULL
AND ar.source_name ='SomeSource'
AND ar.eco_date IN (date '2017-12-22', date '2017-12-21')
AND ar.active = 'Y'
GROUP BY ar.id_external
HAVING COUNT(1) = 1)

最佳答案

假设表t_row_data有列 id_external, id_instance, id_category, source_name, eco_date, active, col7, col8, ... ,您可以像这样重写查询:

select   id_external, null as id_instance, null as id_category,
'SomeSource' as source_name, max(eco_date) as eco_date, 'Y' as active,
max(col7) as col7, max(col8) as col8, ...
from t_row_data
where id_instance is null
and id_category is null
and source_name = 'SomeSource'
and eco_date between date '2017-12-21' and date '2017-12-22'
and active = 'Y'
group by id_external
having count(*) = 1
;

关于sql - 简化嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001877/

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