'SQL query to remove duplicate column values within distinct rows
I am using MS SQL, I have tables "Table1" , "Table2" , "Table3" as mentioned below(might look like a mess, I dont know how to make a table better here). I want the "Expected OutPut" as mentioned ant the bottom.
Want a SQL script to perform this action.
Table 1
FamilyID FamilyName
1 One
2 Two
3 Three
Table2
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
Table3
FamilyID RoomID RoomType
1 1 RoomType1
1 2 RoomType2
2 1 RoomType1
2 2 RoomType2
2 3 RoomType3
2 2 RoomType2
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
2 Name4 RoomType1
Name5 RoomType2
Name6 RoomType3
Name7
Name8
3 Name9 RoomType2
Name10
EDIT: I tried how to show column value only one time if it is repeated and blank until different value comes in sql but the out put is not as i expected. It has duplications as sample shown below for FamilyID=1
Result from 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
Expected OutPut
FamilyID UserName RoomType
1 Name1 RoomType1
Name2 RoomType2
Name3
Solution 1:[1]
You can use a solution like this:
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Munees Majid |
