I am writing query for getting result from multiple table it gives multiple rows with repeating values in all column expect one i want that columns to be short and show in single row, these are my tables
Table Name: AppUesrs
Id Name Gender
1 Goga M
2 Maja M
3 Phadu M
4 Kaku F
5 Seefa F
Table Name: AppProducts
Id Name Value
1 Bc090 10
2 Bc080 15
3 Mc070 2
4 Mc100 16
5 Bc110 15
Table Name: AppOrders
Id Date ExpDate
1 08/9/2014 10/10/2015
2 18/9/2014 08/11/2015
3 20/9/2014 25/12/2015
4 01/10/2014 14/12/2015
5 19/10/2014 15/2/2016
and Table Name: ProductOwners
OId PId UId
1 1 2
1 2 2
1 5 2
2 3 5
3 4 4
3 3 4
3 5 4
3 1 4
I am writing query for it and it show data like this
select O.Id, P.ProductName, U.Name ,O.Date,O.ExpDate
from AppProductOwners PO, AppProducts P, AppOrders O, AppUsers U
Where PO.AppOrderId = O.Id AND PO.ProductsId = P.Id AND PO.AppUserId = U.Id
it is giving data like
O.Id P.Name U.Name O.Date O.ExpDate
1 Bc090 Maja 08/9/2014 10/10/2015
1 Bc080 Maja 08/9/2014 10/10/2015
1 Bc110 maja 08/9/2014 10/10/2015
and i want data like
O.Id P.Name U.Name O.Date O.ExpDate
1 Bc090,Bc080,Bc110 Maja 08/9/2014 10/10/2015
Kindly if any one help me writing this query
If I write it like
SELECT O.ID,
PName = STUFF(( SELECT ',' + P.ProductName
FROM AppProductOwners PO
INNER JOIN AppProducts P
ON P.Id = PO.ProductsId
AND U.Id = PO.AppUserId
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''),
U.FirstName, U.LastName,
O.Date,
o.ExpDate
FROM AppUsers U
INNER JOIN AppOrders O
ON u.id = O.ID;
it shows nothing , and if i write it like
SELECT O.ID,
PName = STUFF(( SELECT ',' + P.ProductName
FROM AppProductOwners PO
INNER JOIN AppProducts P
ON P.Id = PO.ProductsId
AND AppUsers.Id = PO.AppUserId
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''),
U.FirstName, U.LastName,
O.Date,
o.ExpDate
FROM AppUsers U
INNER JOIN AppOrders O
ON u.id = O.ID;
It gives error
Msg 4104, Level 16, State 1, Line 6
The multi-part identifier "AppUsers.Id" could not be bound.