如何从SQL数据库中获取单列名称? [英] how to get single column name from database in sql?

查看:109
本文介绍了如何从SQL数据库中获取单列名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我保存了这样的值

1 AndhraPradesh 22 Vizianagaram Denkada
1 AndhraPradesh 22 Vizianagaram Bhoghapuram
1 AndhraPradesh 22 Vizianagaram Pusapatirega
1 AndhraPradesh 21 Visakhapatnam Bheemunipatnam
1安得拉邦21维沙卡帕特南阿拉库山谷
1安得拉邦21 Visakhapatnam Anakapalle
1安得拉邦21 Visakhapatnam Gajuwaka
1安得拉邦21 Visakhapatnam Chodavaram


但是我想要这样的输出

1 AndhraPradesh 22 Vizianagaram Denkada
博加普拉姆
Pusapatirega
21维沙卡帕特南荒原谷
加朱瓦卡

有可能吗?
谁能解决这个问题
谢谢与问候
Hari

Hi All,
I saved Values like this

1 AndhraPradesh 22 Vizianagaram Denkada
1 AndhraPradesh 22 Vizianagaram Bhoghapuram
1 AndhraPradesh 22 Vizianagaram Pusapatirega
1 AndhraPradesh 21 Visakhapatnam Bheemunipatnam
1 AndhraPradesh 21 Visakhapatnam Araku Valley
1 AndhraPradesh 21 Visakhapatnam Anakapalle
1 AndhraPradesh 21 Visakhapatnam Gajuwaka
1 AndhraPradesh 21 Visakhapatnam Chodavaram


But i want output like this

1 AndhraPradesh 22 Vizianagaram Denkada
Bhogapuram
Pusapatirega
21 Visakhapatnam Araku Valley
Gajuwaka

is it possible r not?
can any one solve it plz
Thanks & Regards
Hari

推荐答案

根据您的插图,我认为您正在寻找的答案是合格的某种".当第一部分重复时,您想要的是一个仅包含最后一列数据的新行.当然,您可以将其他各列留空,但重点是什么.此外,这样做的SQL语句将很丑陋.但是,如果希望将最后一列的所有值都放在一列中(这样每个人将获得一行,并且最后一列中具有n个条目),则可以使用CROSS APPLY语句来实现.这是一个论坛文章,将为您提供帮助:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=102429 [ ^ ]

那篇文章中的声明可能就是您想要的:
Based upon your illustration, I think the answer you are looking for is a qualified "sort of". What it looks like you want is a new row with just the last column''s data when the first part is duplicated. Sure, you could blank the other columns out but what''s the point. Besides, the SQL statement to do so would be ugly. However, if you want all of the values for that last column to be in one column (so each person gets one row and the last column has n number of entries in it), you can do so using a CROSS APPLY statement. Here is a forum post that will help you out:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=102429[^]

The statement from that post that would probably be what you want is this one:
SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM #TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
             FROM #TableChild
             WHERE ASSIGNNUM =p.ASSIGNNUM 
             FOR XML PATH('')) el(EmpList)


显然,您需要对其进行修改以适合您的需求,但最终结果将是最后一列中用逗号分隔的值列表.


Obviously you would need to modify it to fit your needs but the end result would be a comma-delimited list of values in the last column.


这是我的解决方案:

This is my solution:

Select Distinct o.State, o.City,

(

Select I.Mandal + ', ' From Mandal I

Where I.StateID = o.StateID and I.CityID = o.CityID

For XML Path('')

) [Combined Data]

from Country O


这篇关于如何从SQL数据库中获取单列名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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