gpt4 book ai didi

sql - 子查询返回不正确的值

转载 作者:行者123 更新时间:2023-12-04 23:52:07 26 4
gpt4 key购买 nike

首先,我是一名经理,您会看到,我已经有一段时间没有从事此类工作了。但由于各种原因,在我获得更多人手之前,我不得不介绍一些 SQL 编程。是的,我会先声明我在这方面是个无能的白痴。

我有一个非常非常长的 SQL 语句,其中有一大堆来自各种表的选择,以及各种子查询。查询有大约 400 行。它工作正常,直到我尝试添加某个子查询。该子查询返回错误的值。当我将子查询分解为几个较短的测试查询以进行故障排除时,它们返回了正确的值。这是组合的,不起作用。我敢肯定这一定与我加入的方式有关。

我正在尝试获取存储在两个表中的总收入金额:一个表中包含当前值,另一个表中包含历史值。这些值位于客户级别,并且客户表与其他表是一对多的。两个收入表的结构相同,没有共同的记录。一个是另一个的历史文件。我想要做的就是在客户级别上总结两个表之间的收入值。

这是不起作用的子查询。它应该是 current_revenue 和 historical_revenue 的总和:

select c.id1, c.id2,
(select (sum(oe.revenue1)+sum(oe.revenue2)+sum(h.revenue1)+sum(h.revenue2))*.01
from order_entry oe, order_history h
where c.id1 = oe.id1
and c.id2 = oe.id2
and c.id1 = h.id1
and c.id2 = h.id2
and oe.order_type in ('01','02','03','04')
and oe.order_status = 'CLOSED'
and h.order_type in ('01','02','03','04')
and h.order_status = 'CLOSED') as total_revenue
from customer c
where c.id1 = '1234'
and c.id2 = '5678'
--query incorrectly returns $4460
--this query is adding the $1500 in twice (see below)

这是两个有效的测试查询。除了表名外,它们完全相同:

select c.id1, c.id2,
(select (sum(oe.revenue1)+sum(oe.revenue2))*.01
from order_entry oe
where c.id1 = oe.id1
and c.id2 = oe.id2
and oe.order_type in ('01','02','03','04')
and oe.order_status = 'CLOSED') as current_revenue
from customer c
where c.id1 = '1234'
and c.id2 = '5678'
--query correctly returns $1460


select c.id1, c.id2,
(select (sum(h.revenue1)+sum(h.revenue2))*.01
from order_history h
where c.id1 = h.id1
and c.id2 = h.id2
and h.order_type in ('01','02','03','04')
and h.order_status = 'CLOSED') as historical_revenue
from customer c
where c.id1 = '1234'
and c.id2 = '5678'
--query correctly returns $1500

/*
these will be subqueries in another query which needs to return
total revenue = current_revenue + historical_revenue = 1460 + 1500 = 2960
*/

有人能告诉我为什么组合子查询不起作用吗?再一次,我坦率地承认我的愚蠢。我确定以后我会觉得自己像个彻头彻尾的白痴,但我只需要一些帮助。谢谢。

编辑:示例表创建和插入。表格设计得很糟糕。而且很大。因此样本。另请注意,我正在构建的 SQL 语句的庞大之处在于我在选择中为数据馈送提取了大约 10MM 的记录,事实证明这比分解和更新要快。就创建最后可以用 union 连接的多个表而言,没有什么合理的分区依据。我已经尝试过各种方法,但事实证明巨大的选择是最快的。正如您所注意到的,我也不太擅长 SQL 转换,包括优化器提示。

谢谢 Clockwork Muse 的帮助...我将很快测试您的解决方案。此外,没有可用的专用报告工具。

create table customer (id1 varchar2(4),id2 varchar2(4), 
first_name varchar2(30),last_name varchar2(30));

insert into customer values ('1234','5678','DAVID','HOOVER');
insert into customer values ('0676','3724','JOHN','BOWER');
insert into customer values ('7281','1766','ANNA','VALENZUELA');
insert into customer values ('1458','0076','MARK','JACKSON');
insert into customer values ('0003','9783','JESSICA','BURNETT');

create table order_entry (id1 varchar2(4),id2 varchar2(4),
order_no number,order_type varchar2(2),order_status varchar2(10),
revenue1 number(10),revenue2(10));

insert into order_entry values ('1234','5678',238347,'02','CLOSED',1220,0;
insert into order_entry values ('1234','5678',238347,'02','CLOSED',0,240;
insert into order_entry values ('1234','5678',238529,'05','CANCEL',500,700;
insert into order_entry values ('1234','5678',238529,'04','PENDING',871,0;
insert into order_entry values ('0003','9783',198293,'33','CLOSED',870,50;
insert into order_entry values ('0676','3724',219972,'02','CLOSED',375,0;
insert into order_entry values ('0676','3724',219972,'02','PENDING',175,59;
insert into order_entry values ('7281','1766',248221,'04','PENDING',0,999;
insert into order_entry values ('1458','0076',218578,'04','CLOSED',0,99;
insert into order_entry values ('1458','0076',218578,'02','CLOSED',399,0;


create table order_history (id1 varchar2(4),id2 varchar2(4),
order_no number,order_type varchar2(2),order_status varchar2(10),
revenue1 number(10),revenue2(10));

insert into order_history values ('1234','5678',192832,'01','CLOSED',750,0;
insert into order_history values ('1234','5678',192991,'02','CLOSED',0,750;
insert into order_history values ('0003','9783',138982,'01','CLOSED',299,0;
insert into order_history values ('0676','3724',112729,'01','CLOSED',350,0;
insert into order_history values ('1458','0076',185573,'01','CANCEL',1299,199;

最佳答案

最简单的解决方案:如果您已经计算出 2 个正确的值,只需将其相加即可:

( SQLFiddle for test )

select c.id1, c.id2,
(
(select (sum(oe.revenue1)+sum(oe.revenue2)) * 0.01 -- current_revenue
from order_entry oe
where c.id1 = oe.id1
and c.id2 = oe.id2
and oe.order_type in ('01','02','03','04')
and oe.order_status = 'CLOSED'
)
+
(select (sum(h.revenue1)+sum(h.revenue2))*.01 -- historical_revenue
from order_history h
where c.id1 = h.id1
and c.id2 = h.id2
and h.order_type in ('01','02','03','04')
and h.order_status = 'CLOSED'
)
) as total_revenue
from customer c
where c.id1 = '1234'
and c.id2 = '5678'

当然,由于缺乏数据,不可能保证最佳性能,但它只是工作。

关于sql - 子查询返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221125/

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