Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
keen1
Starting Member
21 Posts |
Posted - 2003-09-17 : 13:39:08
|
| i have 4 tables managing the users of an application.Users have their id and their roles and they have some production lines to administer.Also there is a table listing the all the production lines.i need to write a stored proc. returning the lines for them to administer.i need to return all the production lines but i want the production lines they administer at the top of the list!A simple union only brings back all the prod. lines but in no specific order...Below is the tables' genarated scriptsCREATE TABLE [dbo].[DTProdLines] ( [ProdLineID] [smallint] NOT NULL , [ProdLineName] [nvarchar] (4) COLLATE Turkish_CI_AS NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[Roles] ( [RoleID] [int] NOT NULL , [Name] [nvarchar] (50) COLLATE Turkish_CI_AS NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[Users] ( [UserID] [int] IDENTITY (1, 1) NOT NULL , [UserName] [nvarchar] (50) COLLATE Turkish_CI_AS NOT NULL , [RoleID] [int] NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[UsersProdLineAdmin] ( [UserID] [int] NOT NULL , [UserProdLineAdmin] [smallint] NOT NULL ) ON [PRIMARY]GO |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-17 : 13:43:01
|
the only relationship i see between users and product lines is in the "UsersProdLineAdmin" table. What other kind of relationship is there? you haven't provided enough information.sample data would help to.INSERT INTO Tble (col1, col2)SELECT 'something', 'Somethingelse' UNIONSELECT 'something', 'Somethingelse' UNION...etc ...Finally, if you want to order a UNION queries results based on which table it came from, create your own field:SELECT A.* FROM(SELECT 1 as Sort, * FROM tbl1 UNION SELECT 2 as Sort, * FROM tbl2) AORDER BY Sort that will ensure all results from tbl1 are returned BEFORE tbl2.- Jeff |
 |
|
|
keen1
Starting Member
21 Posts |
Posted - 2003-09-17 : 15:29:47
|
| Hi Jeff,that's right; the only relationship between users and product lines is in the "UsersProdLineAdmin" table.my stored proc will return back all the production lines for all the times but what i want is, i want the production lines in the UsersProdLineAdmin table to appear at the top of the list.When i try to use a union, i get the production lines in the UsersProdLineAdmin table at the top of the list but since this is a union i get the same production lines in the return list two times. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-17 : 15:44:19
|
you still have no relationship. how do Users relate to product lines based on the tables you have provided?and PLEASE give some sample data .it sounds like you want to return all rows from 1 table, but if there is a match in another table you want matching rows first.If so:SELECT tbl1.*FROM tbl1LEFT OUTER JOIN tbl2ON tbl1.ID = tbl2.IDORDER BY CASE WHEN tbl2.ID IS NULL THEN 1 ELSE 0 END ASC do you see how this process might be easier if you give some sample data and what you have tried so far instead of me just guessing over and over ....- Jeff |
 |
|
|
keen1
Starting Member
21 Posts |
Posted - 2003-09-18 : 02:24:57
|
| Hi Jeff,i give sample data below:Users table:UserID UserName RoleID------ -------- ------1 oaydogan 22 ntuncel 5UsersProdLineAdmin table:UserID UserProdLineAdmin------ -----------------1 5 1 61 71 21 41 1 2 42 5DTProdLines table:ProdLineID ProdLineName---------- ------------0 CPL1 RCM12 ECL3 BAF4 SPM5 CSL6 CTL7 HDG8 RCM29 CGLNow i want to be able to get the lists below:When i query with the user id=1:UserID ProdLineID ProdLineName LineAdmin------ ---------- ------------ ---------1 5 CSL 11 6 CTL 11 7 HDG 11 2 ECL 11 4 SPM 11 1 RCM1 11 0 CPL 01 3 BAF 01 8 RCM2 01 9 CGL 0And when i query with the user id=2:UserID ProdLineID ProdLineName LineAdmin------ ---------- ------------ ---------2 4 SPM 12 5 CSL 12 0 CPL 02 1 RCM1 02 2 ECL 02 3 BAF 02 6 CTL 02 7 HDG 02 8 RCM2 02 9 CGL 0You see, i want to lines from the UsersProdLineAdmin table at the top and the remaining ones from the DTProdLines table.Regards |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-18 : 15:11:22
|
| [code]SELECT @UserID as UserID, A.ProdlineID, A.ProdLineName, CASE WHEN b.UserProdLineAdmin is null THEN 0 ELSE 1 END as LineAdminFROM ProdLines ALEFT OUTER JOIN UsersProdLineAdmin BON B.UserID = @UserID AND B.UserProdLineAdmin = A.ProdLineIDORDER BY LineAdmin DESC, ProdLineID ASC[/code]- Jeff |
 |
|
|
keen1
Starting Member
21 Posts |
Posted - 2003-09-19 : 03:41:05
|
| Thank you Jeff :) this is so helpful to me. |
 |
|
|
|
|
|
|
|