如何在Rapid SQL中比较一张表中的2行

时间:2018-11-20 16:32:29

标签: sql

我在下面的查询中提取了我需要的所有必需数据。

SELECT a.Name, a.CPN a.Date a.Price a.dt_jay
FROM jatable a
where a.dt_jay = '11/19/2018' -- Previous day
and a.cd_type ='MKT'
and a.cpn between 2.0 and 7.5 
and a.Name  in ('A','B')
Order by a.CPN

我的结果是:

    Name CPN    Date        Price
   A     2.5    12/13/2018  102.1132813
   A     2.5    3/13/2019   101.9804688
   A     2.5    2/13/2019   102.3828125
   A     2.5    1/14/2019   102.2460938
   A     3      12/13/2018  104.2929688
   A     3      3/13/2019   104.1445313
   A     3      2/13/2019   104.0117188
   A     3      1/14/2019   103.8789063
   A     3.5    12/13/2018  105.7226563
   A     3.5    3/13/2019   106.0976563
   A     3.5    2/13/2019   105.9726563
   A     3.5    1/14/2019   105.8476563
   A     4      12/13/2018  106.7773438
   A     4      3/13/2019   106.6523438
   A     4      2/13/2019   106.7148438
   A     4      1/14/2019   106.5898438
   A     4.5    12/13/2018  106.7148438
   A     4.5    3/13/2019   106.7773438
   A     4.5    2/13/2019   106.5898438
   A     4.5    1/14/2019   106.6523438
   B     2.5    12/13/2018  87.609375
   B     2.5    3/13/2019   86.953125
   B     2.5    2/13/2019   87.1875
   B     2.5    1/14/2019   87.40625
   B     3      12/13/2018  90.7265625
   B     3      3/13/2019   90.9609375
   B     3      2/13/2019   91.1796875
   B     3      1/14/2019   91.3828125
   B     3.5    12/13/2018  94.5703125
   B     3.5    3/13/2019   94.65234375
   B     3.5    2/13/2019   94.4140625
   B     3.5    1/14/2019   94.4921875
   B     4      12/13/2018  97.3203125
   B     4      3/13/2019   97.234375
   B     4      2/13/2019   97.1484375
   B     4      1/14/2019   97.41015625
   B     4.5    12/13/2018  99.6953125
   B     4.5    3/13/2019   100.0273438
   B     4.5    2/13/2019   99.9140625
   B     4.5    1/14/2019   99.8046875

我希望最终结果看起来像这样

 Name   CPN  Date       Price       Name    CPN   Date           Price     Price A - Pric B
 A      2.5 12/13/2018  102.1132813 B       2.5   12/13/2018    87.609375   14.50390625
 A      2.5 3/13/2019   101.9804688 B       2.5    3/13/2019    86.953125   15.02734375
 A      2.5 2/13/2019   102.3828125 B       2.5    2/13/2019    87.1875     15.1953125
 A      2.5 1/14/2019   102.2460938 B       2.5    1/14/2019    87.40625    14.83984375
 A       3  12/13/2018  104.2929688 B        3    12/13/2018    90.7265625  13.56640625
 A       3  3/13/2019   104.1445313 B        3     3/13/2019    90.9609375  13.18359375
 A       3  2/13/2019   104.0117188 B        3     2/13/2019    91.1796875  12.83203125
 A       3  1/14/2019   103.8789063 B        3     1/14/2019    91.3828125  12.49609375
 A      3.5 12/13/2018  105.7226563 B       3.5   12/13/2018    94.5703125  11.15234375
 A      3.5 3/13/2019   106.0976563 B       3.5    3/13/2019    94.65234375 11.4453125
 A      3.5 2/13/2019   105.9726563 B       3.5    2/13/2019    94.4140625  11.55859375
 A      3.5 1/14/2019   105.8476563 B       3.5    1/14/2019    94.4921875  11.35546875
 A       4  12/13/2018  106.7773438 B        4    12/13/2018    97.3203125  9.45703125
 A       4  3/13/2019   106.6523438 B        4     3/13/2019    97.234375   9.41796875
 A       4  2/13/2019   106.7148438 B        4     2/13/2019    97.1484375  9.56640625
 A       4  1/14/2019   106.5898438 B        4     1/14/2019    97.41015625 9.1796875
 A      4.5 12/13/2018  106.7148438 B       4.5   12/13/2018    99.6953125  7.01953125
 A      4.5 3/13/2019   106.7773438 B       4.5    3/13/2019    100.0273438 6.75
 A      4.5 2/13/2019   106.5898438 B       4.5    2/13/2019    99.9140625  6.67578125
 A      4.5 1/14/2019   106.6523438 B       4.5    1/14/2019    99.8046875  6.84765625

我想从名称栏中比较A与B,如上所示。将数据与日期和CPN匹配,然后减去价格列(价格A-价格B)

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用条件聚合:

select 'A' as name,
       max(case when a.Name = 'A' then a.CPN end) as a_cpn,        
       max(case when a.Name = 'A' then a.date end) as a_date,        
       max(case when a.Name = 'A' then a.price end) as a_price,        
       max(case when a.Name = 'A' then a.CPN end) as a_cpn,        
       max(case when a.Name = 'B' then a.date end) as b_date,        
       max(case when a.Name = 'B' then a.price end) as b_price,        
       max(case when a.Name = 'B' then a.CPN end) as b_cpn
from jatable a
where a.dt_jay = '11/19/2018' and -- Should use YYYY-MM-DD format
      a.cd_type = 'MKT' and
      a.cpn between 2.0 and 7.5 and
      a.Name  in ('A', 'B')
group by date;