Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 对查询结果进行运算_Sql_Database_Oracle_Plsql - Fatal编程技术网

Sql 对查询结果进行运算

Sql 对查询结果进行运算,sql,database,oracle,plsql,Sql,Database,Oracle,Plsql,我可以对查询结果进行算术运算吗?我的查询返回两行 POSITION_ID DEAL_ID OFFSET CURRENT SWAP SWAP 20756839 5397396 0 22657 27880 20756839 6154345 1 22657 0 同一职位已经达成了两项不同的交易。一个偏移量为1,另一个为0。 现在,我想实现的是: 我需要检查偏移量是否为0,首先我不应该在输出上打印该行

我可以对查询结果进行算术运算吗?我的查询返回两行

POSITION_ID   DEAL_ID   OFFSET  CURRENT SWAP   SWAP
20756839      5397396     0      22657         27880
20756839      6154345     1      22657         0
同一职位已经达成了两项不同的交易。一个偏移量为1,另一个为0。 现在,我想实现的是: 我需要检查偏移量是否为0,首先我不应该在输出上打印该行,其次,将此行的DEAL_ID与另一个具有相同外键的表连接,并从该表中获取闭式交换。最后,我通过加入DEAL_ID从另一个表中得到的已关闭的交易交换减去DEAL_ID 5397396的交换(上面的偏移量为0)

这是获取CLOSED_SWAP值的另一个表

TABLE CLOSE_POSITION_DETAIL_ID
{
   DEAL_ID,
   closed_swap,
   xxx etc
}
例如,上述交易的掉期价值是22657——我从另一个表中获得的已结_掉期


这可能吗?

您可以尝试使用group by并计算不同数量的偏移量。 样本如下:

    select sum(decode(at.did, 11 , -at.swap, at.swap)) Diff from
    (
    select pid , count(distinct offset) from result
    where did in (12, 11)
    group by pid 
    having count(distinct offset)  = 2 ) Gr
    join anotherTable  At
    on AT.did in (11, 12)
假设您的结构如下:

create table result 
(PID number,
 DID number,
 OFFSET number);

 create table anotherTable
 (did number,
  swap number);

 insert into anotherTable values
 (12, 10);

  insert into anotherTable values
 (11, 5);

 insert into result values(
 1, 11,  0);

  insert into result values(
 1, 12, 1);

这是一个非常简单的例子,因为问题并不十分清楚

你希望结果是什么?请编辑你的问题,嗯?请显示预期的输出和“其他表”等。完全不清楚。@user1596226编辑后,它实际上不清楚。也可以写下预期的结果。谢谢你的回答。但是,当我们从结果中获得1000条记录并且需要检查位置ID是否成对(一个偏移量为0,另一个偏移量为1)时,如何使查询工作?根据您的ID添加组。这些行如何?在(12,11)中,你在哪里做的?它是从你以前版本的问题中硬编码的。。。在您当前的示例中,您应该使用POSITION\u ID通过附加连接到主结果来获得它。