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
356 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 15

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now