我想计算公司之间先前协议的数量,并将此计数分配给焦点公司

e3bfsja2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(265)

我的数据库是这样的:(我的数据库截图)

+------------------------+-----------------------------+--------------+---------------+------+
|         p_par          | fk_ior_sdc_alliances_id_par | pcusip_focal | cusip_partner | year |
+------------------------+-----------------------------+--------------+---------------+------+
| "APA Ventures II"      |                         337 | "00185D"     | "032515"      | 1983 |
| "APA Ventures II"      |                         247 | "00185D"     | "46204Q"      | 1985 |
| "APA Ventures II"      |                         178 | "00185D"     | "032515 "     | 1985 |
| "APA Ventures II"      |                         335 | "00185D"     | "00282M "     | 1985 |
| "ARCO Chemical Co"     |                          85 | "001920"     | "45166A"      | 1985 |
| "ARCO Chemical Co"     |                          27 | "001920"     | "816077 "     | 1985 |
| "ARCO Chemical Co"     |                          98 | "001920"     | "22748D "     | 1985 |
| "ARI Technologies Inc" |                          79 | "00204K"     | "49461M "     | 1985 |
| "Abbott Biotech Inc"   |                         334 | "00282M"     | "00185D"      | 1982 |
| "Abbott Biotech Inc"   |                         334 | "00282M"     | " 00185D"     | 1983 |
| "Abbott Biotech Inc"   |                         335 | "00282M"     | "00185D"      | 1985 |
| "Abbott Biotech Inc"   |                         342 | "00282M"     | "89352X"      | 1985 |
| "Abbott Biotech Inc"   |                         329 | "00282M"     | "00790H "     | 1985 |
| "Aall Bank and Trust"  |                          40 | "00291B"     | "040453 "     | 1985 |
| "Abingworth PLC"       |                         103 | "00358Q"     | "370476 "     | 1985 |
| "Abingworth PLC"       |                         312 | "00358Q"     | "53655Q"      | 1985 |
| "Abingworth PLC"       |                         367 | "00358Q"     | "12664T"      | 1985 |
| "Abingworth PLC"       |                          38 | "00358Q"     | "53655Q "     | 1985 |
| "Abingworth PLC"       |                         333 | "00358Q"     | "69643R "     | 1985 |
| "Abingworth PLC"       |                         195 | "00358Q"     | "74763I "     | 1985 |
| "Abingworth PLC"       |                         269 | "00358Q"     | "87926F "     | 1985 |
| "Abingworth PLC"       |                         164 | "00358Q"     | " 108046"     | 1985 |
| "Abingworth PLC"       |                         328 | "00358Q"     | "88117R "     | 1985 |
| "Abingworth PLC"       |                         442 | "00358Q"     | "595141 "     | 1985 |
| "Abingworth PLC"       |                         243 | "00358Q"     | "34416D "     | 1985 |
| "Abingworth PLC"       |                         237 | "00358Q"     | "827056 "     | 1985 |
| "Abingworth PLC"       |                         198 | "00358Q"     | "12465I "     | 1985 |
+------------------------+-----------------------------+--------------+---------------+------+

我有一个焦点公司和它与其他公司的协议。大多数协定是双边的(这里只指一个伙伴),但也有多边的。我要做的是计算同一公司之间的事先协议。例如,在 firts 划去焦点公司的 cusip 是74961d(pcusip\U par),合伙人是50590p。我想数一数1980年以前这种组合发生过多少次。
如果是多边协议,我打算进行组合,如focal-partner\u 1、focal-partner\u 2、focal\u partner\u 3等。然后我将计算总和,并将此数字分配给focal firm(一行)。我不考虑合伙人之间的协议。
预期数据:

+----------------------+-----------------------------+--------------+---------------+------+------------------+
|        p_par         | fk_ior_sdc_alliances_id_par | pcusip_focal | cusip_partner | year | prior_agreements |
+----------------------+-----------------------------+--------------+---------------+------+------------------+
| "APA Ventures II"    |                         337 | "00185D"     | "032515 "     | 1983 |                0 |
| "APA Ventures II"    |                         178 | "00185D"     | "032515 "     | 1985 |                1 |
| "Abbott Biotech Inc" |                         334 | "00282M"     | " 00185D"     | 1982 |                0 |
| "Abbott Biotech Inc" |                         334 | "00282M"     | " 00185D"     | 1983 |                1 |
| "Abbott Biotech Inc" |                         335 | "00282M"     | " 00185D"     | 1985 |                2 |
+----------------------+-----------------------------+--------------+---------------+------+------------------+

我相信有一个代码来解决这个问题。
致以最诚挚的问候,法里德

aurhwmvo

aurhwmvo1#

我相信有一个代码来解决这个问题。
当然有。一般来说,你应该发布你已经尝试过的问题代码。另外,在图片中发布数据,而不是以其他人可以复制的形式提供数据,这是一种不好的形式。你也只需要发布最少的数据,有人将需要回答你的问题。
例如,在第一行中,焦点公司的cusip是74961d(pcusip\U par),合作伙伴的cusip是50590p。我想数一数1980年以前这种组合发生过多少次。
这很简单:只需将您的条件添加到 WHERE 子句,并使用 COUNT 聚合函数:

SELECT COUNT(*) AS agreement_count
FROM agreements
WHERE
    PCUSIP_par = '74961D'
    AND partner_cusip_1 = '50590P'
    AND sdc_year < 1980

如果是多边协议,我打算进行组合,如focal-partner\u 1、focal-partner\u 2、focal\u partner\u 3等。然后我将计算总和,并将此数字分配给focal firm(一行)。我不考虑合伙人之间的协议。
我不太清楚你在问什么。如果你澄清你的逻辑并提供一些预期的数据。
尽管如此,您的数据模型确实需要重新审视。当您有多个名称几乎相同的列时(就像您使用 partner_cusip_1 , partner_cusip_2 ,和 partner_cusip_3 ). 对于有4个以上参与方的协议,您必须开始添加列,这将大大增加开销(编写查询、降低性能和更容易出错)。
我建议使用另一种模式:存储基本协议信息(例如 sdc_year 可能是“焦点”公司),然后将合作伙伴信息存储在另一个表中:

CREATE TABLE agreements
(
    agreement_id SERIAL PRIMARY KEY,
    sdc_year INT -- or have this be a TIMESTAMP, 
    PCUSIP_focal -- add a foreign key if you have a table of CUSIPs somewhere
)

CREATE TABLE agreement_partners
(
    agreement_id REFERENCES agreements(agreement_id),
    partner_cusip, -- add a foreign key if you have a table of CUSIPs somewhere
    PRIMRY KEY (agreement_id, partner_cusip)
)

使用此表结构,您的初始查询(用于计算 74961D 以及 50590P 在1980年之前,您的查询变成:

SELECT COUNT(*) AS agreement_count
FROM agreements a
    INNER JOIN agreement_partners ap ON
        ap.agreement_id = a.agreement_id
WHERE
    a.sdc_year < 1980
    AND a.pcusip_focal = '74961D'
    AND ap.partner_cusip = '50590P'

相关问题