Link to home
Start Free TrialLog in
Avatar of stu215
stu215Flag for United States of America

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of stu215

ASKER

I dont think i'm going to get to this today but hope to be able to play with it again tomorrow.
Avatar of stu215

ASKER

Worked like a charm! :-)

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

XML PATH('')),2, 200000 )
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.