User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 428,202 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,231 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums

Collating results of subqueries into fields

Join Date: May 2008
Posts: 3
Reputation: Paul Phillips is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Paul Phillips Paul Phillips is offline Offline
Newbie Poster

Collating results of subqueries into fields

  #1  
May 16th, 2008
Hi Guys,

I'm new here and have come across a perplexing problem. I have a user table

  1. CREATE TABLE Users
  2. (
  3. UserID VARCHAR(64),
  4. UserName VARCHAR(50)
  5. )
  6.  
  7. INSERT INTO Users (UserName) VALUES ('Tom')
  8. INSERT INTO Users (UserName) VALUES ('Dick')
  9. INSERT INTO Users (UserName) VALUES ('Harry')

Each user has roles and groups.

  1. CREATE TABLE Roles
  2. (
  3. RoleID VARCHAR(64),
  4. RoleName VARCHAR(50)
  5. )
  6.  
  7. INSERT INTO Roles (RoleName) VALUES ('Noob')
  8. INSERT INTO Roles (RoleName) VALUES ('Average')
  9. INSERT INTO Roles (RoleName) VALUES ('Admin')
  10.  
  11. CREATE TABLE Groups
  12. (
  13. GroupID VARCHAR(64),
  14. GroupName VARCHAR(50)
  15. )
  16.  
  17. INSERT INTO Groups (GroupName) VALUES ('Fishing')
  18. INSERT INTO Groups (GroupName) VALUES ('Running')
  19. INSERT INTO Groups (GroupName) VALUES ('Base Jumping')

Linked via a couple of reference tables

  1. CREATE TABLE UsersRoles
  2. (
  3. UserID VARCHAR(64),
  4. RoleID VARCHAR(64)
  5. )
  6.  
  7. INSERT INTO UsersRoles (UserID,RoleID) VALUES (1,1)
  8. INSERT INTO UsersRoles (UserID,RoleID) VALUES (1,2)
  9. INSERT INTO UsersRoles (UserID,RoleID) VALUES (1,3)
  10. INSERT INTO UsersRoles (UserID,RoleID) VALUES (2,1)
  11. INSERT INTO UsersRoles (UserID,RoleID) VALUES (3,1)
  12. INSERT INTO UsersRoles (UserID,RoleID) VALUES (3,3)
  13.  
  14. CREATE TABLE UsersGroups
  15. (
  16. UserID VARCHAR(64),
  17. GroupID VARCHAR(64)
  18. )
  19.  
  20. INSERT INTO UsersGroups (UserID,GroupID) VALUES (1,1)
  21. INSERT INTO UsersGroups (UserID,GroupID) VALUES (1,2)
  22. INSERT INTO UsersGroups (UserID,GroupID) VALUES (2,2)
  23. INSERT INTO UsersGroups (UserID,GroupID) VALUES (3,1)
  24. INSERT INTO UsersGroups (UserID,GroupID) VALUES (3,3)

Now the tricky part

I am trying to get out a recordset along the lines of:

User Name Roles Groups
======= ================= =====================
Tom Noob, Average, Admin Fishing, Running
Dick Noob Running
Harry Noob, Admin Fishing, Base Jumping


Due to the constraints of Crystal Reports I can not use user defined SQL Functions. Can anybody help?


Kind regards,


Paul
Last edited by peter_budo : May 16th, 2008 at 6:36 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 3:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC