Author |
Topic |
Khus
Starting Member
2 Posts |
Posted - 2013-09-08 : 03:27:46
|
HiI have a table likeNumber Desc1 Bank2 Shop3 Store 2 Home1 Mall2 HouseI want to have a result asNumber Desc All1 Bank Mall2 Shop Home House3 StoreCan you someone help to do this using a proper select syntax |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-08 : 05:08:21
|
How do you plan to make sure "Shop" is handled before "Home" which should be handled before" House"? Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
Khus
Starting Member
2 Posts |
Posted - 2013-09-08 : 06:29:42
|
Thank you for the reply.I will have a field for the order likeNumber Desc Order1 Bank 12 Shop 13 Store 12 Home 21 Mall 22 House 3 quote: Originally posted by SwePeso How do you plan to make sure "Shop" is handled before "Home" which should be handled before" House"? Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
|
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-08 : 13:44:37
|
[code]DECLARE @Sample TABLE ( Number TINYINT NOT NULL, [Desc] VARCHAR(100) NOT NULL, [Order] TINYINT NOT NULL );INSERT @SampleVALUES (1, 'Bank', 1), (2, 'Shop', 1), (3, 'Store', 1), (2, 'Home', 2), (1, 'Mall', 2), (2, 'House', 3);-- SwePesoSELECT n.Number, STUFF(CAST(f.Data AS VARCHAR(MAX)), 1, 1, '') AS [Desc All]FROM ( SELECT Number FROM @Sample GROUP BY Number ) AS nCROSS APPLY ( SELECT ' ' + x.[Desc] FROM @Sample AS x WHERE x.Number = n.Number ORDER BY x.[Order] FOR XML PATH(''), TYPE ) AS f(Data);[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|