Solved

CFGRID :: How do I query a table but have one column display as a list from another table

Posted on 2013-01-30
5
372 Views
Last Modified: 2013-02-01
I have a CFGRID in which I am trying to do the following and do not know if this can be accomplished with SQL only or if there is a way that I could do it in my coldfusion cfc.

This is what the return statement looks like at the moment:
<cfreturn QueryConvertForGrid(getEmps, page, pageSizeToUse)>

Open in new window

- If done with in coldfusion how do I return a modified data set to the cfgrid?

- else how do i accomplish the following in SQL only?

I want to select * from a table of employees and then add on a field called Roles which is stored in a seperate table and has a 1-N relationship.  The Roles table only contains IDs for the employee and role. The UserRoles table contains the Role name.

Table 1= Employee Info
Table 2= Roles - Contains role names
Table 3= UserRoles - Contains the roles a user has

1 Employee has many roles

They are connected by:

Employees.EmpID=Roles.EmpRoleID
Roles.Role_ID=UserRoles.rID

I want the output to be one line per employee with roles displaying as a comma delimited list.

Example:

EmpName | Phone             | Email           | Roles
--------------------------------------------------------------------------------------
John Doe   | 555-555-5555 | xx@xx.com | Role1, Role2, Role3, ...



This is as far as I've gotten ( it gets the roles for one emp if I enter an ID for EmpID ):
It doesnt include the other info i need from the employees table.

DECLARE @listStr varchar(500)
SELECT @listStr = LTRIM(RTRIM(COALESCE(@listStr+', ', '') + [Role]))
FROM roles, userRoles
WHERE Role_ID=rID
	AND EmpID=...?...
SELECT @listStr as List

Open in new window

0
Comment
Question by:stu215
[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
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 38837811
It could be done w/CF or SQL. In SQL Server you can use the xmlpath trick to generate a CSV list for each employee. It works well for moderate sized tables.

Here's a complete example with sample data. I didn't quite follow the FK names, so I used "EmpID" and and "RoleID" across the board :) Adjust them as needed.

Data Sample
declare @Employees table (
EmpID int identity
, EmpName varchar(50)
, Phone varchar(20)
, Email varchar(20)
)

declare @UserRoles  table (
RoleID int
, RoleName varchar(100)
)

declare @Roles  table (
EmpID int
, RoleID int
)

insert into @Employees (EmpName, Phone, Email)
select 'John Doe', '555-555-5555', 'xx@xx.com' union all
select 'Bob Smith', '111-111-1111', 'yy@xx.com' union all
select 'Jane Doe', '222-222-2222', 'zz@xx.com'

insert into @UserRoles (RoleID, RoleName)
select 1, 'Role 1' union all
select 2, 'Role 2' union all
select 3, 'Role 3' union all
select 4, 'Role 4' union all
select 5, 'Role 5' 

insert into @Roles (EmpID, RoleID)
select 1, 1 union all
select 1, 3 union all
select 1, 5 union all
select 2, 2 union all
select 2, 4

Open in new window


The SQL
SELECT e.EmpName
              , e.Phone
              , e.Email
              , SUBSTRING( ( SELECT ',' + ur.RoleName
                                            FROM   @Roles r INNER JOIN @UserRoles ur ON r.RoleID = ur.RoleID
                                           WHERE  r.EmpID = e.EmpID 
                                           ORDER BY ur.RoleName
                                           FOR XML PATH('')),2, 200000 ) AS RoleList
FROM  @Employees e

Open in new window


Results
EmpName   Phone                   Email               RoleList
John Doe	555-555-5555	xx@xx.com	Role 1,Role 3,Role 5
Bob Smith	111-111-1111	yy@xx.com	Role 2,Role 4
Jane Doe	222-222-2222	zz@xx.com	NULL

Open in new window

0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 38838241
Hi stu,

I d not know about your query result set but if you want to return the values as comma separated in the cfgrid column, you can always use the valuelist function
0
 
LVL 10

Author Comment

by:stu215
ID: 38839942
I dont think i'm going to get to this today but hope to be able to play with it again tomorrow.
0
 
LVL 10

Author Closing Comment

by:stu215
ID: 38844697
Worked like a charm! :-)

-- What do the parameters on the XML_Path denote? 2, 200000

XML PATH('')),2, 200000 )
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38844809
They're actually for substring(expression ,start , length ), not xml path. xml path returns a big string with a leading comma. Starting at "2" skips the extra comma and the 200000 is just a ridiculously large number to make sure you get the whole string.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can I skip a node in XML? 9 31
SQL- GROUP BY 4 22
MS SQL Server Management Studio R2 4 27
SQL 2012 Instance Problem 3 43
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

740 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