March 10, 2008 at 1:32 am
Hi all,
I have 2 tables as below
Company: id, CompanyName
Employee: id, CompanyID, EmployeeName
as you guys can see that Employee.CompanyID is a foreign key link to Company table.
For some reason I have to write a query that will get me the result like this
CompanyName1 EmployeeName1 EmployeeName2 EmployeeName3
CompanyName2 EmployeeName1 EmployeeName2
CompanyName3 EmployeeName1
instead of
CompanyName1 EmployeeName1
CompanyName1 EmployeeName2
CompanyName2 EmployeeName3
CompanyName2 EmployeeName1
CompanyName3 EmployeeName2
CompanyName4 EmployeeName1
How can achieve this , thanks alot
March 10, 2008 at 3:47 am
Whoknow (3/10/2008)
Hi all,I have 2 tables as below
Company: id, CompanyName
Employee: id, CompanyID, EmployeeName
as you guys can see that Employee.CompanyID is a foreign key link to Company table.
For some reason I have to write a query that will get me the result like this
CompanyName1 EmployeeName1 EmployeeName2 EmployeeName3
CompanyName2 EmployeeName1 EmployeeName2
CompanyName3 EmployeeName1
instead of
CompanyName1 EmployeeName1
CompanyName1 EmployeeName2
CompanyName2 EmployeeName3
CompanyName2 EmployeeName1
CompanyName3 EmployeeName2
CompanyName4 EmployeeName1
How can achieve this , thanks alot
This should not really be done in SQL, but you have two main choices. One is to return the result using a single column (all employee names in one column, separated by commas):
SELECT c.CompanyName
, STUFF(( SELECT DISTINCT TOP 100 PERCENT
',' + t2.EmployeeName
FROM Employee AS t2
WHERE t2.CompanyID = c.id
ORDER BY ',' + t2.EmployeeName
FOR
XML PATH('')), 1, 1, '')
FROM Company c
or if you know the max number of employees your query can return, you can return the data using this format (you need to use PIVOT and row_number). This second solution has the major limitation that if you have more employees later, you will need to change the schema of the returned information.
Anyway, the above is something that should really be done in your client application 🙂
Regards,
Andras
March 10, 2008 at 4:44 am
It's gonna be a mess if you have, say, 1000 employees... you said you have to do this "for some reason"... what is the reason?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2008 at 2:56 pm
Hi,
The simplest would be by using PIVOT function, as i undestand in general scenario there will n.Of employees for a given company. So its pretty hard write a query like this and at some point you will hit into an issue where SELECT clause can't support the no.of columns that you are looking for.
So tell us how many employees you have it on company by company basis and why you want to do this? what exactly your requirement.
For using Pivot, you can check out the below link
http://www.devx.com/dbzone/Article/28165
Thanks -- Vj
March 10, 2008 at 5:00 pm
Pivot also has some limits... I believe you can only Pivot based on one column...
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2008 at 6:48 pm
Jeff Moden (3/10/2008)
Pivot also has some limits... I believe you can only Pivot based on one column...
Not only is the new pivot command limited to one column, it's limited to one operation on one column. Meaning if you want the average of some values, and the sum of some other values - that's two separate pivot operations (=2 table scans, twice the reads, etc.... and twice the fun of course).
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
March 10, 2008 at 6:56 pm
There's just nothing like someone providing instant confirmation for us folks just finally getting into 2k5... 🙂 Thanks, Matt.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 10, 2008 at 7:55 pm
This is something that you should definitely do in the presentation layer. You're pretty much making extra columns in your resultset which the presentation layer than happens to present in a convenient format. Most reporting tools (Crystal and SQL Server's both fit the bill) can easily do this for you.
March 10, 2008 at 8:40 pm
Hi all,
thanks for helping me out about this problem, There are only about 5 employees each company so the number of employee is not an issue here , let me try the query out and have a look of PIVOT function too , I'll get back later when I have any problem . But again thanks a lot 🙂
July 21, 2009 at 12:20 am
Hi
were you able to work this out?
I need to do something similar
thanks
Fred
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply