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 391,687 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,207 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:
Views: 610 | Replies: 2
Reply
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  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Collating results of subqueries into fields

  #2  
May 19th, 2008
First of all, you must add PRIMARY keys and FOREIGN keys as well.
I don't know of crystal's limitations. Using standard sql you can clue those tables together by join operations, like this select * from A join B on A.field = B.field...
If crystal is unable to understand modern joins you can also use tradition inner joins like
select * from A, B where A.field = B.field...
Last edited by tesuji : May 19th, 2008 at 7:32 am.
Reply With Quote  
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

Re: Collating results of subqueries into fields

  #3  
May 19th, 2008
No offense, Friend, but your answer is waffle.

Identifying primary and foreign keys only helps to speed up the engine and enforce a level of data integrity. Neither apply to my problem.

The way I thought I could do it is like this (I know the names are different - I wanted to keep it simple for the example) :

  1. CREATE TABLE #TempPPUser
  2. (
  3. UserID VARCHAR(64),
  4. Groups VARCHAR(8000),
  5. Roles VARCHAR(8000)
  6. )
  7.  
  8. INSERT INTO #TempPPUser
  9. SELECT tps_guid UserID, '' AS Groups, '' AS Roles FROM tps_user
  10.  
  11. DECLARE @UserID varchar(64), @GroupID varchar(64), @RoleID varchar(64)
  12.  
  13. DECLARE usersgroups_cursor CURSOR FOR
  14. SELECT tps_user_id, tps_group_id
  15. FROM tps_user_group
  16.  
  17. OPEN usersgroups_cursor
  18.  
  19. FETCH NEXT FROM usersgroups_cursor
  20. INTO @UserID, @GroupID
  21.  
  22. UPDATE #TempPPUser
  23. SET Groups = COALESCE(Groups + ',', '') + (SELECT tps_group.tps_name FROM tps_group WHERE tps_guid = @GroupID)
  24. WHERE #TempPPUser.UserID = @UserID
  25.  
  26. WHILE @@FETCH_STATUS = 0
  27. BEGIN
  28. FETCH NEXT FROM usersgroups_cursor
  29. INTO @UserID, @GroupID
  30.  
  31. UPDATE #TempPPUser
  32. SET Groups = COALESCE(Groups + ',', '') + (SELECT tps_group.tps_name FROM tps_group WHERE tps_guid = @GroupID)
  33. WHERE #TempPPUser.UserID = @UserID
  34. END
  35.  
  36. CLOSE usersgroups_cursor
  37. DEALLOCATE usersgroups_cursor
  38.  
  39. DECLARE usersroles_cursor CURSOR FOR
  40. SELECT tps_user_id, tps_role_id
  41. FROM tps_user_role
  42.  
  43. OPEN usersroles_cursor
  44.  
  45. FETCH NEXT FROM usersroles_cursor
  46. INTO @UserID, @RoleID
  47.  
  48. UPDATE #TempPPUser
  49. SET Roles = COALESCE(Roles + ',', '') + (SELECT tps_role.tps_name FROM tps_role WHERE tps_guid = @RoleID)
  50. WHERE #TempPPUser.UserID = @UserID
  51.  
  52. WHILE @@FETCH_STATUS = 0
  53. BEGIN
  54. FETCH NEXT FROM usersroles_cursor
  55. INTO @UserID, @RoleID
  56.  
  57. UPDATE #TempPPUser
  58. SET Roles = COALESCE(Roles + ',', '') + (SELECT tps_role.tps_name FROM tps_role WHERE tps_guid = @RoleID)
  59. WHERE #TempPPUser.UserID = @UserID
  60. END
  61.  
  62. CLOSE usersroles_cursor
  63. DEALLOCATE usersroles_cursor
  64.  
  65. SELECT tps_user.tps_name, #TempPPUser.Groups, #TempPPUser.Roles
  66. FROM tps_user INNER JOIN #TempPPUser ON tps_user.tps_guid = #TempPPUser.UserID
  67. WHERE tps_name = 'PPhillips1'
  68.  
  69. DROP TABLE #TempPPUser
  70.  
  71. END

=======================

The problem is it takes forever to run! Anyone any better ideas?
Last edited by peter_budo : May 19th, 2008 at 12:46 pm. Reason: Keep It Organized - please use [code] tags. No offence but code tags are there for reason
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 2:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC