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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2016 SQL Licensing 7 41
Get the latest status 8 32
Inserting oldest record into new table. 5 25
Place a Case statement or a if else statement in a where clause 7 20
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

803 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