让我先说我是一名经理,我已经有一段时间没有做过这种工作了,你会看到的。但由于各种原因,我不得不覆盖一些 SQL 编程,直到我获得更多员工为止。是的,我会提前说我在这方面是个无能的白痴。
我所拥有的是一个非常非常长的 SQL 语句,其中包含来自各种表的一大堆选择,以及各种子查询。该查询有大约 400 行。它工作得很好,直到我尝试添加某个子查询。该子查询返回错误的值。当我将子查询分解为几个较短的测试查询以进行故障排除时,它们会返回正确的值。组合起来是行不通的。我确定这一定与我加入的方式有关。
我正在尝试获取存储在两张表中的总收入金额:一张表中有当前值,另一张表中有历史值。这些值位于客户级别,并且客户表与其他两个表是一对多的。这两个收入表的结构相同,没有共同的记录。一个是另一个的历史档案。我要做的就是在客户级别上汇总两个表之间的收入值。
这是不起作用的子查询。它应该是 current_revenue 和 history_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 语句的规模很大,因为我在 select 中提取了大约 10MM 记录作为数据馈送,结果证明这比分解和更新要快。就创建最终可以与联合连接的多个表而言,分区没有任何合理性。我尝试了各种方法,但结果是巨大的选择是最快的。正如您所注意到的,我也不是很擅长 SQL 转换,包括优化器提示。
感谢发条缪斯的帮助......我很快就会测试你的解决方案。此外,没有可用的专用报告工具。
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;