如何在SQL中的运算符之间应用多个

时间:2014-09-22 11:46:57

标签: sql oracle

我有一个日记帐类型表,其中包含AccountCredit_DebitAmount列。我正在尝试 select结果应该看起来像一行,然后是对应的行数。金额列为数字/十进制。

预期产出:一个金额,后面是相等的相反(加或减)金​​额

expected output

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

所以,我的查询下面抛出了无效的关系运算符

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

这个返回空结果集

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

3 个答案:

答案 0 :(得分:1)

您的第二个将使用OR运算符。

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

但是它会显示包含介于-1000和1000之间的数量的行减去介于-100和100之间的数量的行。

要做你想做的事,如果我做对了,你可以起诉:

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

答案 1 :(得分:1)

好的,这已经奏效了。感谢大家的努力。

select * from table where amount between 100 AND 1000
UNION
select * from table where amount between -1000 AND -100

答案 2 :(得分:0)

试试这个:

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