Solved

help needed in sql statement

Posted on 2013-10-23
5
311 Views
Last Modified: 2013-10-23
hi EE

i have two tables "Employee" and "Phone"
Table Employee

EmployeeID | Name
_____________ |  ______
1                       AAA
2                        BBB

Table Phone

EmployeeID  PhoneNO
1                      123
1                       456
1                       789
2                         555

what i need that when i make sql statement between the two table i need the result to be like this

EmployeeID  PhoneNo
1                    123 , 456 ,789
2                      555

i dont want to get all the rows as the normal case

thanks
0
Comment
Question by:Kamal Khaleefa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 39593569
if MySQL, you can use the GROUP_CONCAT function
0
 
LVL 10

Accepted Solution

by:
ienaxxx earned 300 total points
ID: 39593570
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 39593595
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 39593635
Here's one one to use 'for xml path'; using cross apply
SELECT
*
FROM employee
CROSS APPLY(
            SELECT STUFF(
                          (
                           SELECT ','+ cast(phone.PhoneNO AS varchar)
                           FROM    Phone
                           WHERE   employee.EmployeeID = phone.EmployeeID
                           FOR XML PATH('')
                           )
                         ,1,1,'')
            ) AS ca1 (phones)

Open in new window

it produces this result:
| EMPLOYEEID | NAME |      PHONES |
|------------|------|-------------|
|          1 |  AAA | 123,456,789 |
|          2 |  BBB |         555 |

Open in new window

0
 
LVL 16

Author Closing Comment

by:Kamal Khaleefa
ID: 39593654
thanks
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question