如何用sql语句计算出变化的share?

数据文件q4.csv:

cat q4.csv    
nameofissuer,shares
nvr inc,11112
pool corp,598689
jefferies finl group inc,433558
constellation brands inc,5624324
bank amer corp,680233587
occidental pete corp,264178414

数据文件q3.csv:

cat /tmp/q3.csv
nameofissuer,shares
nvr inc,11112
pool corp,404057
jefferies finl group inc,433558
bank amer corp,797683307
occidental pete corp,255281524
vanguard index fds,43000
spdr s&p 500 etf tr,39400
ulta beauty inc,24203

准备工作:

create database project;
create table q4(nameofissuer  text,shares int);
copy q4(nameofissuer,shares)
from '/tmp/q3.csv' delimiter ',' csv header;
create table q3(nameofissuer  text,shares int);
copy q3(nameofissuer,shares)
from '/tmp/q3.csv' delimiter ',' csv header;  

使用full outer join:

select           
    q4.nameofissuer as issuer_q4,q4.shares as shares_end,q3.nameofissuer as issuer_q3,q3.shares as shares_start from q4
full outer join q3
on q4.nameofissuer  = q3.nameofissuer;

        issuer_q4         | shares_end |        issuer_q3         | shares_start 
--------------------------+------------+--------------------------+--------------
 bank amer corp           |  680233587 | bank amer corp           |    797683307
 constellation brands inc |    5624324 |                          |             
 jefferies finl group inc |     433558 | jefferies finl group inc |       433558
 nvr inc                  |      11112 | nvr inc                  |        11112
 occidental pete corp     |  264178414 | occidental pete corp     |    255281524
 pool corp                |     598689 | pool corp                |       404057
                          |            | spdr s&p 500 etf tr      |        39400
                          |            | ulta beauty inc          |        24203
                          |            | vanguard index fds       |        43000

我希望得到的结果:

     nameofissuer         | shares_end | shares_start |   change   
--------------------------+------------+--------------+------------
 bank amer corp           |  680233587 |    797683307 | -117449720
 constellation brands inc |    5624324 |            0 |    5624324
 jefferies finl group inc |     433558 |       433558 |          0
 nvr inc                  |      11112 |        11112 |          0
 occidental pete corp     |  264178414 |    255281524 |    8896890
 pool corp                |     598689 |       404057 |     194632
 spdr s&p 500 etf tr      |          0 |        39400 |     -39400
 ulta beauty inc          |          0 |        24203 |     -24203
 vanguard index fds       |          0 |        43000 |     -43000

我可以用vba在excel中完成,也可以用python实现,但是可否用纯粹的postgres语句来实现?

阅读 164
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进