[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
399 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
  • 2
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

872 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