例如,我有一个名为tb_Item
的表----------------------------------
|Item_Ref|Main_Item_Ref|Type|Rate|
|--------+-------------+----+----|
|1234ABC |MNJDH |Sub |0 |
|MNJDH |MNJDH |Main|98 |
----------------------------------
基本上,我想要实现的是获取Main项的Rate值并将其放在Sub Item / s上。
在此示例表中,项目1234ABC如何从其主项MNJDH获得98率并返回如下行:
----------------------------------
|Item_Ref|Main_Item_Ref|Type|Rate|
|--------+-------------+----+----|
|1234ABC |MNJDH |Sub |98 |
----------------------------------
谢谢。
答案 0 :(得分:1)
您可以使用自我加入来执行此操作:
select i.*, m.Rate
from tb_item i left join
tb_item m
on i.main_item_ref = m.main_item_ref and
m.type = 'Main'
where i.type = 'Sub';
答案 1 :(得分:1)
自我内心联接
select t1.Item_Ref, t1.Main_Item_ref, t1.type,t2.Rate
from #temptable t1
inner join #temptable t2
on t1.Main_Item_ref = t2.Main_Item_ref
and t2.type = 'Main'
and t1.type ='Sub'