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
370 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 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

838 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