Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 sql help

Author  Topic 

digit
Starting Member

1 Post

Posted - 2014-07-03 : 21:41:22
SELECT Users.ID, Users.Name, Users.Address, Users.Phone, Users.Email, Users.Password, Dogs.DID, Dogs.DogAge, Dogs.DogBreed, Dogs.DogName, Papers.PID, Papers.URLtoPaper
FROM Users
INNER JOIN Dogs
ON Users.ID=Dogs.ID
INNER JOIN Papers
ON Users.ID=Papers.ID
WHERE Users.ID = 1


1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 1 papers.html
1 David 14623 995 qwill@outlook.com *** 2 3 Sharpei Wondering 1 papers.html


Ok, As you can see the USER part of these two returned rows are the same.
I want this to be ONE row with the Dogs.DID, Dogs.DogAge, Dogs.DogBreed, Dogs.DogName ordered next to each other from first to last.

In this example, the desired effect is to be
1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 2 3 Sharpei Wondering 1 papers.html

help?

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-07-04 : 01:07:17
[code]
;With Users
AS
(
SELECT 1 as ID , 'David' as Name,'14623' as Address, '995'as Phone,'qwill@outlook.com' as Email,'***' as Password
)
,
Dogs
AS
(SELECT 1 as ID, 1 as DID,2 as DogAge, 'Rottweiler' AS DogBreed, 'Barney' AS DogName UNION ALL
SELECT 1 as ID,2,3,'Sharpei','Wondering'
)
,
Papers
AS
(SELECt 1 as ID, 1 as PID , 'papers.html' as URLtoPaper)




SELECT
Users.ID, Users.Name, Users.Address,
Users.Phone, Users.Email, Users.Password,
Dogs.DogsSTR,
Papers.PID, Papers.URLtoPaper
FROM Users
CROSS APPLY
(
SELECT
' ' + CAST(DID as VARCHAR(30)) + ' '
+ CAST(DogAge AS VARCHAR(30)) + ' '
+ CAST(DogBreed AS VARCHAR(30)) + ' '
+ CAST(DogName AS VARCHAR(30))
FROM
Dogs
WHERE
Dogs.ID = Users.ID
FOR XML PATH('')
)AS Dogs(DogsSTR)

INNER JOIN Papers
ON Users.ID=Papers.ID
WHERE Users.ID = 1
[/code]


output:
[code]
ID Name Address Phone Email Password DogsSTR PID URLtoPaper
1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney 2 3 Sharpei Wondering 1 papers.html
[/code]


sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-07-04 : 01:09:43
You can add a
,




SELECT
Users.ID, Users.Name, Users.Address,
Users.Phone, Users.Email, Users.Password,
STUFF(Dogs.DogsSTR,1,1,'') as DogsSTR,
Papers.PID, Papers.URLtoPaper
FROM Users
CROSS APPLY
(
SELECT
', ' + CAST(DID as VARCHAR(30)) + ' '
+ CAST(DogAge AS VARCHAR(30)) + ' '
+ CAST(DogBreed AS VARCHAR(30)) + ' '
+ CAST(DogName AS VARCHAR(30))
FROM
Dogs
WHERE
Dogs.ID = Users.ID
FOR XML PATH('')
)AS Dogs(DogsSTR)

INNER JOIN Papers
ON Users.ID=Papers.ID
WHERE Users.ID = 1



[/code]
ID Name Address Phone Email Password DogsSTR PID URLtoPaper
1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney, 2 3 Sharpei Wondering 1 papers.html
[/code]


sabinWeb MCP
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2014-07-04 : 01:12:20
output:

ID Name Address Phone Email Password DogsSTR PID URLtoPaper
1 David 14623 995 qwill@outlook.com *** 1 2 Rottweiler Barney, 2 3 Sharpei Wondering 1 papers.html



sabinWeb MCP
Go to Top of Page
   

- Advertisement -