SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 SQL Server Administration (2012)
 sql help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

digit
Starting Member

1 Posts

Posted - 07/03/2014 :  21:41:22  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 07/04/2014 :  01:07:17  Show Profile  Reply with Quote

;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



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

stepson
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 07/04/2014 :  01:09:43  Show Profile  Reply with Quote
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



Go to Top of Page

stepson
Constraint Violating Yak Guru

Romania
434 Posts

Posted - 07/04/2014 :  01:12:20  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000