这是我的问题:
我有下表(截断)
ID CustomerNumber ResellerID
1 12 NULL
2 56 1
作为更大查询的一部分 - 我正在这样做:
select customernumber,
case when ResellerID = id then customernumber end as 'Parent Account'
from table1
我想要得到的是具有 resellerID 的行,将其与 ID 表匹配并输出 customernumber,因此结果如下所示:
CustomerNumber Parent Account
12 NULL
56 12
而且我无法让查询很好地发挥作用 - 我试过运行外部左连接,但我只得到 NULL 值 - 所以我在某处缺少一些语法和逻辑。
你应该使用 SELF JOIN
通过使用原始表的 ResellerID 列和第二个表的 ID 列创建别名来连接同一个表。
select t1.customernumber, t2.customernumber as 'ParentAccount'
from table1 t1 LEFT JOIN table1 t2 ON t1.ResellerID = t2.ID
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句