473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

SQL Query aggregate phone numbers

Hi, i have a question, imagine that i have a table with names and phone numbers, like this:

Name | Phone
Jack | 2457349843
Jack | 9347842344
Mary | 4324324423
John | 3123123123
John | 43243244324

I want a query that returns something like this

Name | Phone
Jack | 2457349843, 9347842344
Mary | 4324324423
John | 3123123123, 43243244324

Thanks in advance, i really need this.
Nov 22 '09 #1
1 1534
nbiswas
149 100+
Try this(Sql Server 2005+)
-- Table declarations with sample data
Expand|Select|Wrap|Line Numbers
  1. declare @t table(name varchar(50),phone bigint)
  2. insert into @t 
  3.     select 'Jack', 2457349843 union all select 'Jack', 9347842344 union all
  4.     select 'Mary', 4324324423 union all
  5.     select 'John', 3123123123 union all select 'John', 43243244324
--Program Starts
Expand|Select|Wrap|Line Numbers
  1. select name,LEFT(Phone,LEN(Phone) -1) as Phone from (
  2. select t2.name,
  3.         ( 
  4.             select cast(phone as varchar(max)) + ','
  5.             from @t t1
  6.             where t1.name = t2.name
  7.             for XML path('')
  8.         ) as Phone
  9. from @t t2
  10. group by t2.name)X(name,Phone)
Output
----------

name Phone
Expand|Select|Wrap|Line Numbers
  1. Jack    2457349843,9347842344
  2. John    3123123123,43243244324
  3. Mary    4324324423
Nov 23 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dan Gidman | last post by:
Okay all I have a problem. I have two list of adresses and phone numbers. I do not have control over the contents of the lists The only unique field between the two is the phone number. I need...
8
by: Bruce Dodds | last post by:
When run manually, my query appends 14 records. When run from code using CurrentProject.Connection.Execute, the same query appends 11 records. Does anyone have an explanation of why this could be...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
8
by: Thomas A. Anderson | last post by:
I have ran into a rut! I have google'd, Yahoo'd, but could not find the proper syntax. I have spent over two day trying to figure this out, and decided that I will have to ask for help from the...
2
by: Welie | last post by:
I am trying to do a query to produce a list of employee and their telephone numbers (IF ANY) which joins on 3 tables. The complication is the 3rd table, the table containing telephone numbers....
3
by: rogynskyy | last post by:
Hi guys, I'm running MSDE 2000 A on Win XP I've got a database with several tables, all of the tables display data in query manager. I wrote this simple query: Select
6
by: Phil Stanton | last post by:
I am running a query that calls a function used to format addresses depending on the width of a control on a report that shows that address. The same query is used as the RecordSource of lots of...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
1
by: deepu03 | last post by:
Hi All I have the following query to be tuned.. SELECT distinct a.EMPLID ,a.PER_ORG ,a.HIRE_DT ,a.grade
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.