我正在使用MS SQL,我有如下所述的表“Table1”,“Table2”,“Table3”(可能看起来像一团糟,我不知道如何在这里更好地制作表格)。 我想要提到底部的“预期的OutPut”。
希望SQL脚本执行此操作。
表1
FamilyID FamilyName
1 One
2 Two
3 Three
表2
FamilyID UserID UserName
1 1 Name1
1 2 Name2
1 3 Name3
2 4 Name4
2 5 Name5
2 6 Name6
2 7 Name7
2 8 Name8
3 9 Name9
3 10 Name10
表3
FamilyID RoomID RoomType
1 1 RoomType1
1 2 RoomType2
2 1 RoomType1
2 2 RoomType2
2 3 RoomType3
2 2 RoomType2
预期OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
2 Name4 RoomType1
Name5 RoomType2
Name6 RoomType3
Name7
Name8
3 Name9 RoomType2
Name10
修改 我试过how to show column value only one time if it is repeated and blank until different value comes in sql ,但输出并不像我预期的那样。如下所示,对于FamilyID = 1
,它有重复 来自how to show column value only one time if it is repeated and blank until different value comes in sql 的结果
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType1
Name3 RoomType1
Name1 RoomType2
Name2 RoomType2
Name3 RoomType2
预期OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
答案 0 :(得分:0)
您可以使用这样的解决方案:
SELECT T1.FamilyID,T2.UserName,T3RoomType FROM Table1 T1
JOIN Table2 T2 ON T1.FamilyID =T2.FamilyID
LEFT JOIN Table3 T3 ON T2.UserID =T3.RoomID
GROUP BY T2.UserID