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
 General SQL Server Forums
 New to SQL Server Programming
 How to get results in one row from multiple record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Niki
Yak Posting Veteran

51 Posts

Posted - 07/19/2013 :  18:06:05  Show Profile  Reply with Quote
I have following situation -

DECLARE @table1 TABLE
(
Stuid INT NOT NULL,
schoolcd varchar(10),
enterDate Datetime,
entercd char(10)
)

INSERT @Student VALUES
(4648, ‘20130515’, ‘892’,’30’),
(4648, ‘20130103’, ‘764,’10’)

SELECT STATEMENT will give me following -

Stuid schoolcd entercd enterDate
4648 892 30 05/15/2013
4648 764 10 01/03/2013

Desired output is one row for two or more records for same stuid
4648 892 30 05/15/2013 764 10 01/03/2013 xxx xx xx/xx/xxxx

how can I accomplish that?
Thanks,

Niki

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/19/2013 :  20:52:32  Show Profile  Reply with Quote
Here is a way:

DECLARE @Student TABLE
(
Stuid INT NOT NULL, 
schoolcd varchar(10),
enterDate Datetime,
entercd char(10)
);

INSERT @Student(Stuid, enterDate, schoolcd, entercd) VALUES
(4648, '2013-05-15', '892','30'),
(4648, '2013-01-03', '764','10');


;WITH CTE AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY Stuid Order by enterDate) as RN FROM @student)
SELECT T1.Stuid, T1.enterDate, T1.schoolcd, T1.entercd, T2.enterDate, T2.schoolcd, T2.entercd 
	FROM CTE T1 LEFT JOIN CTE T2 ON T1.Stuid = T2.Stuid and T1.RN = T2.RN-1 WHERE t1.RN = 1


Go to Top of Page

Niki
Yak Posting Veteran

51 Posts

Posted - 07/22/2013 :  11:56:21  Show Profile  Reply with Quote
Thank you, MuMu88!

Niki
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 07/22/2013 :  11:58:21  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Not for two OR MORE rows...


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 07/22/2013 :  12:05:01  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE @Student TABLE
(
	Stuid INT NOT NULL, 
	schoolcd varchar(10),
	entercd char(10),
	enterDate Datetime
)

INSERT @Student VALUES
(4648, '892', '30', '20130515'),
(4648, '764', '10', '20130103');

-- SwePeso
SELECT		d.StuID,
		f.Data
FROM		(
			SELECT DISTINCT	StuID
			FROM		@Student
		) AS d
CROSS APPLY	(
			SELECT		' ' + RTRIM(s.SchoolCD) + ' ' + RTRIM(s.EnterCD) + ' ' + CONVERT(VARCHAR(10), s.EnterDate, 101)
			FROM		@Student AS s
			WHERE		s.StuID = d.StuID
			ORDER BY	s.SchoolCD
			FOR XML		PATH('')
		) AS f(Data)
ORDER BY	d.StuID;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/22/2013 :  12:24:11  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

DECLARE @Student TABLE
(
	Stuid INT NOT NULL, 
	schoolcd varchar(10),
	entercd char(10),
	enterDate Datetime
)

INSERT @Student VALUES
(4648, '892', '30', '20130515'),
(4648, '764', '10', '20130103');

-- SwePeso
SELECT		d.StuID,
		f.Data
FROM		(
			SELECT DISTINCT	StuID
			FROM		@Student
		) AS d
CROSS APPLY	(
			SELECT		' ' + RTRIM(s.SchoolCD) + ' ' + RTRIM(s.EnterCD) + ' ' + CONVERT(VARCHAR(10), s.EnterDate, 101)
			FROM		@Student AS s
			WHERE		s.StuID = d.StuID
			ORDER BY	s.SchoolCD
			FOR XML		PATH('')
		) AS f(Data)
ORDER BY	d.StuID;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA


Depends on whether OP wants values in single or separate columns.
If former, above is fine

If latter dynamic sql may be what Op is after

as in

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.08 seconds. Powered By: Snitz Forums 2000