主表:Select * from Table1
Id Name1 Name2
101 ttt sss
第二张表:Select * from Table2
SId Id Colum1 Column2 Column3
1 101 hhh xxx erre
2 101 wsa tgf fdfd
第三张表:Select * from Table3
TId Id TColumn1 Tcolumn2
5 101 uyt uyu
我的查询:
Select * from Table1 t1
Join Table2 t2 on t2.Id= t1.Id
Join Table3 t3 on t3.Id= t1.Id
我正在像这样打算数据
Id Colum1 Column2 Column3 TColumn1 Tcolumn2
1 hhh xxx erre uyt uyu
2 wsa tgf fdfd uyt uyu
我想要的是我的数据
Id Colum1 Column2 Column3 TColumn1 Tcolumn2
1 hhh xxx erre uyt uyu
2 wsa tgf fdfd null null
答案 0 :(得分:1)
您可以使用以下查询
; with cte as
(
Select
Colum1,
Column2,
Column3,
TColumn1,
Tcolumn2,
row_number () over(partition by t2.id order by SId asc) r
from Table1 t1
Join Table2 t2 on t2.Id= t1.Id
Join Table3 t3 on t3.Id= t1.Id
)
Select
Colum1,
Column2,
Column3,
TColumn1 =case when r=1 then TColumn1 else NULL end,
Tcolumn2 =case when r=1 then Tcolumn2 else NULL end
from cte
的 See working demo 强>