Hi PRA,
Refer below SQL Qurey where you get the exact output.
SQL
DECLARE @MainTable AS TABLE (Id INT,Name VARCHAR(100))
INSERT INTO @MainTable VALUES(1,'Rustam')
INSERT INTO @MainTable VALUES(2,'Firuz')
INSERT INTO @MainTable VALUES(3,'Asror')
INSERT INTO @MainTable VALUES(4,'Nurullo')
DECLARE @RealtedTable AS TABLE (Id INT,IdPerson INT,Food VARCHAR(500),Fruit VARCHAR(500),Vegetable VARCHAR(500),Water VARCHAR(500))
INSERT INTO @RealtedTable VALUES(1,1,'Oil','apple','','')
INSERT INTO @RealtedTable VALUES(2,1,'','','tomato','RC-cola')
INSERT INTO @RealtedTable VALUES(3,2,'Oil','apple','','')
INSERT INTO @RealtedTable VALUES(4,2,'','','tomato','RC-cola')
INSERT INTO @RealtedTable VALUES(5,3,'Oil','apple','','')
INSERT INTO @RealtedTable VALUES(6,3,'','','tomato','RC-cola')
SELECT IdPerson
,(SELECT Name
FROM @MainTable main
WHERE main.Id = rbl.IdPerson ) Person
,MAX(Food) AS Food
,MAX(Fruit) AS Fruit
,MAX(Vegetable) AS Vegetable
,MAX(Water) As Water
FROM @RealtedTable rbl
GROUP BY IdPerson
OutPut
IdPerson |
Person |
Food |
Fruit |
Vegetable |
Water |
1 |
Rustam |
Oil |
apple |
tomato |
RC-cola |
2 |
Firuz |
Oil |
apple |
tomato |
RC-cola |
3 |
Asror |
Oil |
apple |
tomato |
RC-cola |