0

I have a journal type table where it has Account, Credit_Debit and Amount columns. I am trying a select where by the result should look like a row followed by its contra amount row. Amount column is Numeric/Decimal.

Expected output: An amount followed by its equivalent opposite(plus or minus) amount

expected output

|Account|c_d|Amount|
| 1     |D  | 100  |
! 7     |C  | -100 |
| 8     |D  | 750  |
| 1     |C  | -750 |
| 10    |C  | -500 |
| 11    |D  | 500  |

So, my query below threw invalid relational operator

select * from table where amount between 100 and 1000 AND (-100) - (-1000)

This one returned empty result set

select * from table where 
 (amount between('100') AND ('1000')) AND (amount between('-100') AND ('-1000'))
6
  • 1
    Is the presented sample of data expected output(looks like it) or the source data? If it's expected result, it'll be a good idea to post a sample of source data. Commented Sep 22, 2014 at 11:55
  • 1
    I think you have to change the AND in middle of the amount statements to OR. Commented Sep 22, 2014 at 11:56
  • 1
    between('100') AND ('1000')) is really bad coding style. First numbers should not be enclosed in single quotes. '100' is a character literal (aka "string"), not a number. 100 is a number literal. Secondly: the parentheses around the numbers ('-100') useless. Commented Sep 22, 2014 at 11:58
  • Is the AMOUNT column a character string, or is it a NUMBER? In the last comparison you've made the literal values all character string by putting them inside apostrophes. If AMOUNT is a character string you're probably going to have real problems getting your comparisons to work. Commented Sep 22, 2014 at 11:59
  • Updated the question with datatype and expected output Commented Sep 22, 2014 at 12:01

3 Answers 3

1

Ok, this has worked. Thanks for everyone's efforts.

select * from table where amount between 100 AND 1000
UNION
select * from table where amount between -1000 AND -100
Sign up to request clarification or add additional context in comments.

Comments

1

Your second one will work with an OR operator.

select * from table where (amount between('100') AND ('1000')) OR (amount between('-100') AND ('-1000'))

But it will show rows containing amounts between -1000 and 1000 minus those with amounts between -100 and 100.

To do what you want to do, if I get it right, you can sue:

SELECT T1.amount, T2.amount from table T1, table T2 WHERE ABS(T1.amount) = ABS(T2.amount)

Comments

0

Try this:

select * from table where 
 (amount between('100') AND ('1000')) AND (amount between('-1000') AND ('-100'))

        

2 Comments

Please could you read my question and the things I have tried
Use OR not AND while joining the two where clauses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.