gpt4 book ai didi

sql - 表之间的差异,其中关键字段上的 1 到 3 关系

转载 作者:行者123 更新时间:2023-12-04 05:49:41 24 4
gpt4 key购买 nike

我正在尝试使用 SQL 服务器创建一个表,该表创建了一个两者之间的差异表。我在其他帖子中没有找到的关于此的棘手部分是一张表是单个“帐户”数据,如下所示:

TABLE A      
Account Security ID Sec Name Shares
------- ----------- --------- ------
1 Sec1 Security1 20000
1 Sec2 Security2 50000
1 Sec3 Security3 10000
1 Sec4 Security4 35000

和我想比较的数据是一组“帐户”(在本例中为 3):
TABLE B        
Parent_acct Account Security ID Sec Name Shares
----------- ------- ----------- --------- ------
Clone 200 Sec1 Security1 15000
Clone 200 Sec3 Security3 22000
Clone 200 Sec4 Security4 8000
Clone 300 Sec1 Security1 11000
Clone 300 Sec3 Security3 8500
Clone 300 Sec4 Security4 11200
Clone 400 Sec1 Security1 16000
Clone 400 Sec2 Security2 7800
Clone 400 Sec3 Security3 3500

我需要一些 sql 来定位表 A 中包含的每个帐户的表 B 中缺少的安全 ID

例如,鉴于上面的两个表,我希望下面的输出告诉我缺少的安全性以及缺少的帐户。

希望这是有道理的。

我已经使用游标查询来生成我需要的数据,但是,我正在尝试创建一些可以轻松应用于 Crystal Reports 的东西,而游标查询对此没有用处。

预先感谢您查看此内容。
Output    
Account Security ID Sec Name
(From B) (From A) (From A)
------- ----------- ---------
200 Sec2 Security2
300 Sec2 Security2
400 Sec4 Security4

到目前为止我的查询:
select * FROM
(SELECT * from
(select p.acct_cd, s.ext_sec_id, s.sec_name, p.qty_sod
from csm_Security s, cs_position p
where s.sec_id = p.sec_id
and p.acct_cd = '329'
and s.sec_typ_cd in ('COM','FSTK','ADR')) A
cross join
(select distinct child_acct_cd
cs_fund_config fc
where fc.parent_acct_cd IN ('clone_as')) B) AAccounts,
(select fc.child_acct_cd, s.ext_sec_id, s.sec_name, p.qty_sod
from csm_Security s, cs_position p, cs_fund_config fc
where s.sec_id = p.sec_id
and fc.child_acct_cd = p.acct_cd
and fc.parent_acct_cd IN ('clone_as')
and s.sec_typ_cd in ('COM','FSTK','ADR')) BAccounts
where AAccounts.ext_sec_id *= BAccounts.ext_sec_id
and AAccounts.child_acct_cd *= BAccounts.child_acct_cd

最佳答案

棘手的一个!试试这个(我无法测试,注意语法错误):

SELECT BAccounts.Account, A.SecurityID, A.SecName
from TableA A
cross join (select distinct Account
from TableB) BAccounts
left outer join TableB B
on B.Account = BAccounts.Account
and B.SecurityID = A.SecurityID
where B.SecurityID is null

逻辑是:
  • 从表A开始
  • 将每一行与在表 B 中找到的所有可能的帐户连接起来
  • 现在使用 B 的帐户和 A 的 SecurityId
  • 将它与表 B 进行左外连接。
  • 如果未找到,则 B.SecurityId 为空,我们通过子查询
  • 获得违规帐户

    关于sql - 表之间的差异,其中关键字段上的 1 到 3 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231394/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com