我有一个返回不同结果的组查询,第一次返回错误结果,然后返回正确结果。
select
c.customer_name, c.customer_address, invoice_no,
sum(nvl(invoiced_amount,0)) invoice_amount,
sum(nvl(received_amount,0)) received_amount,
sum(nvl(returned_amount,0)) returned_amount
from sales_detail d, sales s, customer c
where d.inquiry_id = s.inquiry_id
and s.customer_id = c.customer_id
and s.sale_date between '01-jan-2012' and '31-dec-2012'
group by c.customer_name, c.customer_address, invoice_no
它返回多行并提供正确的行数,但会使某些发票的价值翻倍,通常在它第一次运行时发生,然后给出正确的结果。
下面是一个例子:
数据库中的实际数据
invoice_no | invoiced_amount | received_amount | returned_amount
000010020 | 500 | 230 | 0
查询返回结果
invoice_no | invoiced_amount | received_amount | returned_amount
000010020 | 1000 | 460 | 0
查询几天前开始表现得像这样,否则工作得很好。
附加信息
- sales_detail 表上有 5 个索引
- 客户是一个视图,物理表在另一个同名模式中
环境信息
- 操作系统:Windows 2003 服务器
- 数据库:Oracle 9.0.1.1 企业版