【发布时间】:2015-11-11 04:28:29
【问题描述】:
表 A:
- AccountID(PK,int,非空)
- 代理 (varchar(50), null)
- AccountType (varchar(50), null)
样本表:
╔═══════════╦════════════════╦═══════╗
║ AccountID ║ AccountType ║ Agent ║
╠═══════════╬════════════════╬═══════╣
║ 413393 ║ Invoice ║ A ║
║ 417811 ║ Credit ║ NULL ║
╚═══════════╩════════════════╩═══════╝
表 B:
- AccountID(int, not null) - 这是外键,我根据匹配的 AccountID 记录从两个表中提取数据。
- Ref_AccountID (int, null)
样本表:
╔═══════════╦════════════════╦
║ AccountID ║ Ref_AccountID ║
╠═══════════╬════════════════╬
║ 413393 ║ NULL ║
║ 417811 ║ 413393 ║
╚═══════════╩════════════════╩
说明: 如果 AccountType 是 invoice,那么会有一个 Agent 与之关联。从表 A 可以看出,它与代理 A 相关联。
电流输出:
╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║ 413393 ║ Invoice ║ NULL ║ A ║
║ 417811 ║ Credit ║ 413393 ║ NULL ║
╚═══════════╩═════════════╩═══════════════╩═══════╝
预期输出:
╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║ 413393 ║ Invoice ║ NULL ║ A ║
║ 417811 ║ Credit ║ 413393 ║ A ║
╚═══════════╩═════════════╩═══════════════╩═══════╝
应根据 Ref_AccountID 显示代理。在本例中,Ref_AccountID 为 413393,对于表 A 中的此 AccountID,Agent 为“A”。
谢谢
【问题讨论】:
-
对我来说这看起来像是一张桌子而不是两张.....
标签: sql sql-server