简体   繁体   中英

Oracle 9.0.1.1 - Same query returning different results

I have a group query which returns different results, first time it returns wrong result and then returns correct result.

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

It returns multiple rows and provide correct rows count but doubles the value of some invoices, usually it happens when it runs first time and later gives correct results.
Following is an example:

Actual data in database

invoice_no | invoiced_amount | received_amount | returned_amount
000010020  |   500           |      230        |      0

Result return by the query

invoice_no | invoiced_amount | received_amount | returned_amount
000010020  |   1000          |      460        |      0  

Query started behaving like this some days ago, otherwise was working perfectly.

Additional information

  • There are 5 indexes on sales_detail table
  • Customer is a view, physical table is in another schema with the same name

Environment information

  • OS: Windows 2003 Server
  • Database: Oracle 9.0.1.1 Enterprise

This is not a question we can answer. You have a simple GROUP BY query and it will sum those attributes in a consistent fashion. If you get different results it is because the underlying data is different when you run the query.

You provide a sample of the "actual data" but neglect to say which table it comes from. Let's assume it is the sales_detail table. If there are records in a table which sum to X but the query returns an aggregated value of 2 X you must have double counting. That is, the joins in the query are causing you to get two instances of each sales_detail record.

Now you also say this is a transient phenomenon. If so, then it seems likely that something is temporarily spawning a second version of some record. Perhaps you have an EOD process which generates a additional customer record for some unfathomable reason, and then later clears it down - or closes off the old one?

This is the limit of the help we can give you. This is not an Oracle problem, this is some peculiarity in your database. You can see the data, we can't. You know - or should know - the business processes, we don't.

So all we can do is give you some pointers.

"usually it happens when it runs first time and later gives correct results."

How have you established this pattern? "Usually" is the same as "always": you're going to need more precision to help you solve this. What investigation have you done to discover why the results vary? For instance, have you established what other other activity is happening when you run this query? Have you listed out the raw data before you run the query?

"Query started behaving like this some days ago, otherwise was working perfectly."

So what changed a few days ago?


"Invoices have been checked individually"

Yes but you have a multiple table join. Spurious records or poorly-written conditions can result in the same record being joined more than once.


"it might be a bug"

Possibly. You're using the initial production release of a major version, and those are notoriously prone to bugs. However, it seems unlikely that software, even a point zero version, should suddenly start exhibiting buggy behaviour if - as you assert - nothing has changed.

But even if you're right and it is a bug, that knowledge doesn't help you. You're using the base release of Oracle 9iR1, a version of the product which has been obsolete for over a decade. So obviously your organisation isn't keen on upgrading, and presumably doesn't pay for Support so patching is not an option (even if Oracle would furnish you with patches for such old software, which they won't).

So, if it is a bug you'll need to track it down yourself and come up with a workaround yourself. Good luck with that.

在我看来,无论是客户表还是销售表都有重复或者连接不在PK上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM