列出明智的排出去 [英] Column wise out put to row wise out put

查看:65
本文介绍了列出明智的排出去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,



以下是我的选择查询的输出

Hi sir,

The below one is the out put of my select query

CityName1	CityName2
Bangalore	Delhi
Delhi		Mumbai
Mumbai		Bangalore



我需要显示为


I need to display it as

Bangalore - Delhi - Mumbai - Bangalore



请帮助我实现这一目标。



谢谢


Please help me for achieving this.

Thank you

推荐答案

DECLARE @route varchar(max)



DECLARE @route varchar(max)

SELECT
    @route = ISNULL(@route, '') + CASE WHEN @route IS NULL THEN '' ELSE ' - ' END + 
        CityName1
FROM
    your_table





添加到最后你需要的东西......如果你描述得更好并显示你的查询我可以使它更精确



Add to that whatever you need at the end...if you describe a bit better and show your query I can make it more precise


试试这个:



Try this:

declare @route nvarchar(max),@route1 nvarchar(max)
select @route = CityName1 from tbl_map where ID=1//here get your cityname1 top1
SELECT @route1 =STUFF((SELECT '-' + cast(CityName2 as varchar) AS [text()] FROM tbl_map  FOR XML PATH('') ), 1, 1, '' )  
select @route+'-'+@route1


这篇关于列出明智的排出去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆