Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with query retuning only records where value > 0

Status
Not open for further replies.

andreadd

MIS
Jan 15, 2008
67
0
0
Hello,

I am writing a query and part of my select state is this: SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) as MINE

now this works well but returns all values and I am only after values greater than 0. i am familiar with MSSQL but very rusty with oracle syntax. I tried using a case statement and I tried to add in the where clause to only return those records where MINE is > 0 but I keep messing it up.

can anyone help?

thanks!
 

We can't see what you see and our crystal ball is not working today.
Post the full SQL statement.
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Andreadd said:
Code:
...SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) as MINE
...where MINE [B][I]is[/I][/B] > 0

but I keep messing it up.

I'll bet that the errors you are receiving are either:

Code:
ORA-00904: "MINE": invalid identifier

or (if you incorrectly have the word "is" in front of your ">" operator):

Code:
ORA-00908: missing NULL keyword

...right?

If your error is the first one, above, then the problem is that "MINE" is an expression alias. Oracle recognizes expression aliases only as an "ORDER BY" reference (e.g., "...ORDER BY MINE") or when an outer SQL statement refers to the alias that a sub-query defines:

Code:
SELECT * FROM
    (SELECT <your other expressions>
           ,SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) as MINE
       FROM <table_name(s)>
    )
 WHERE MINE > 0;

If your error is the second error, above, then remove the extraneous "IS", then see Error 1, above.

Let us know if this solves your problem.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel.
 
when i inserted SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) > '0' in the WHERE statement I got error -cannot perform GROUP. so I tried to do case when SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT)> '0' then SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) as MINE
else null in the select statement I got needed right parens (remember i do mssql well, not this) then after adding too many parens i got missing FROM error

what I ended up doing in the end is inserting a HAVING clause after my group by statement:
having (SUM (A.INVOICE_AMOUNT) != B.INVOICE_AMOUNT)

my thinking is that if there is a difference greater than 0 then these 2 values would not be equal. it appears to work
 
add the following clause after any group by or where and before any order by

having SUM (A.INVOICE_AMOUNT) - B.INVOICE_AMOUNT) > 0

Bill
Lead Application Developer
New York State, USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top