473,791 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting data from multiple rows into one column

I have a table that has values as follows:
PersonID Degree
55 MD
55 Phd
55 RN
60 MD
60 Phd

I need a create a query that will give me output like this:

PersonID Degree
55 MD, Phd, RN
60 MD, Phd

Any ideas

Dec 3 '05
16 43360
> In SQL Server 2005 we can do it in one very simple statement utilising FOR
XML extensions, this makes for less code, less complexity and the logic is
coded once in a central location - do you not agree that is good?


Tony,

how much is the output from FOR XML taxing the network bandwidth?
Most of the xml I'm dealing with is at least 50 times smaller when
zipped.

Dec 5 '05 #11
The beuty about this fellow, eg..

select type,
(
select name + ',' as [text()]
from sys.objects soi
where soi.type = t.type
order by name
for xml path( '' ), root( 'sysobjects' ), type
)
from ( select distinct type from sys.objects ) as t

Will give output like this...

D <sysobjects>DF_ _spt_value__sta tu__436BFEE3,</sysobjects>
IT
<sysobjects>que ue_messages_100 3150619,queue_m essages_1035150 733,queue_messa ges_1067150847, </sysobjects>
P <sysobjects>sp_ MSrepl_startup, sp_MScleanupmer gepublisher,</sysobjects>
S
<sysobjects>sys rowsetcolumns,s ysrowsets,sysal locunits,sysfil es1,syshobtcolu mns,</sysobjects>
SQ
<sysobjects>Que ryNotificationE rrorsQueue,Even tNotificationEr rorsQueue,Servi ceBrokerQueue,</sysobjects>
U
<sysobjects>spt _fallback_db,sp t_fallback_dev, spt_fallback_us g,spt_monitor,s pt_values,</sysobjects>

Which isn't XML, in fact take the root off and you are just left with the
concatenated data - no tags, its an extension to the FOR XML just for this
purpose which is requested a lot.

Taking the ROOT off gives...

D DF__spt_value__ statu__436BFEE3 ,
IT
queue_messages_ 1003150619,queu e_messages_1035 150733,queue_me ssages_10671508 47,
P sp_MScleanupmer gepublisher,sp_ MSrepl_startup,
S
sysallocunits,s ysasymkeys,sysb inobjs,sysbinsu bobjs,syscerts, syschildinsts,s ysclsobjs,sysco lpars,sysconvgr oup,sysdbfiles, sysdbreg,sysder cv,sysdesend,sy sendpts,sysfile s1,sysftinds,sy sguidrefs,sysho btcolumns,sysho bts,sysidxstats ,sysiscols,sysl nklgns,syslogsh ippers,sysmulti objrefs,sysnsob js,sysobjkeycry pts,sysobjvalue s,sysowners,sys privs,sysqnames ,sysremsvcbinds ,sysrmtlgns,sys rowsetcolumns,s ysrowsetrefs,sy srowsets,sysrts ,sysscalartypes ,sysschobjs,sys serefs,syssingl eobjrefs,syssql guides,systyped subobjs,sysuser msgs,syswebmeth ods,sysxlgns,sy sxmitqueue,sysx mlcomponent,sys xmlfacet,sysxml placement,sysxp rops,sysxsrvs,
SQ
EventNotificati onErrorsQueue,Q ueryNotificatio nErrorsQueue,Se rviceBrokerQueu e,
U
MSreplication_o ptions,seqnumbe rs,spt_fallback _db,spt_fallbac k_dev,spt_fallb ack_usg,spt_mon itor,spt_values ,

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"Alexander Kuznetsov" <AK************ @hotmail.COM> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
In SQL Server 2005 we can do it in one very simple statement utilising
FOR
XML extensions, this makes for less code, less complexity and the logic
is
coded once in a central location - do you not agree that is good?


Tony,

how much is the output from FOR XML taxing the network bandwidth?
Most of the xml I'm dealing with is at least 50 times smaller when
zipped.

Dec 5 '05 #12
Tony,

that's impressive

Dec 5 '05 #13
Impressive - now that's an understatement - its blumin fantastic!

Can you imagine the amount of coding and complexity it replaces! And the
best thing about it is that it scales and performs really well too.

Itzik Ben-Gan showed me it and since I've played with it, its become one of
those you can use it everywhere solutions :)

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"Alexander Kuznetsov" <AK************ @hotmail.COM> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Tony,

that's impressive

Dec 5 '05 #14
Thanks for the great ideas everyone.

Tony,
I tried to implement your code but every time I get an error msg, it
doesn't accept elements after "FOR XML"

Dec 6 '05 #15
Its SQL Server 2005 only - are you using 2005?

Tony

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"bika" <ae*****@gmail. com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Thanks for the great ideas everyone.

Tony,
I tried to implement your code but every time I get an error msg, it
doesn't accept elements after "FOR XML"

Dec 6 '05 #16

Thank you for your response. I too needed to violate years of SQL
theory and flatten out a table into one row per person. Our need is to
easily include and exclude people for solicitation based on their role.
Your statement got reduced a half dozen views down to one and the
performance is better.

Thanks again!
*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4693
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the data, and to keep life simple I want to reduce the dimensions of the matrix so that I have no missing values, since not all the algorithms are able to handle them and there is sufficient redundancy in the variables that I can afford to lose...
15
3828
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I couldn't do it using reqular transact SQL. The problem is that this procedure is taking longer and longer to run. Up to 5 hours now! It is anaylizing about 30,000 records. I think partly because we add new records every month. The procedure...
1
1667
by: Yama | last post by:
Hi, I am really confused. I have created a strong typed dataset for Northwind database Customer table. Now I am loading it with a stream of XML (ADO style) with the following: Customers _cust = new Customers(); XmlTextReader xmlReader = new XmlTextReader(stream); stream.Position = 0;
2
23945
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table but the table column names. I've seen other posts that suggest using the SQL command DESCRIBE but I can't get it to work for some reason. Other posts have code samples but they're written in VB which I am not familiar with. I
6
2248
by: melanieab | last post by:
Hi, Easy question. It seems to me that I'm following the examples correctly, but apparently I'm not. I'm trying to retrieve the data from a row called "row" and a column called "File". This is what I have: (xFile is the int value in column File and tCat is the table) First I try: xFile = int.Parse((tCat.Rows).ToString()); Another example I found:
1
5291
by: Craig Banks | last post by:
If a row of data in a dataset has a lot of columns the row displaying the data in a datagrid will run way off the screen. What I'd like to do is display a row of data over several datagrid rows so the user doesn't have to scroll horizontally. Essentially, I want to wrap a datagrid row (not text in individual columns) with as much control as possible. Make sense? While this seems simple enough on the surface, I can't figure out how to do...
11
2252
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to reflect that the 3 items have been deleted only to discover that the 3 items appear, however when I click on them to display their information which runs a datareader over the same database it appears that the data has now gone. I wondered whether...
4
6020
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over data from August or any prior month for that matter works fine which is why this is so weird. Note that June 2004 through today is stored in the same f_pageviews table. Nothing has changed on the server in the last couple of months. I upgraded...
6
2726
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added action buttons, such as "Edit", "Add", and "Comment". Since I do not know how many rows of data will be retrieved - and therefore how many buttons I need - I am using button arrays for each button, like so: echo "<input type=\"submit\"...
6
2946
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1) Constraint pk_table1 Primary Key,
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.