Solved

Join tables

Posted on 2011-09-15
3
290 Views
Last Modified: 2012-05-12
I have a select statement for a menu control that I need to join to another table to allow permission dynamically

I am using a CTE select.
now my appid are the different menu categoryies that will display the menu.

select * from
(
SELECT u.UserID,
         ga.AppPermission,
         a.Title,
         a.Url,
         a.Description,
         a.AppStatus,
         a.ParentId,
         a.Appid,
a.OrdinalPosition,
      row_number() over (partition by a.Title, a.Url, a.Description order by ParentId, OrdinalPosition, Title) rn
    FROM Users u
  JOIN UserGroup_new ug ON u.UserKey = ug.UserKey
  JOIN GroupApplications_test ga ON ug.GroupID = ga.GroupID
  JOIN site_SiteMap_beta a ON ga.AppID = a.AppID
WHERE u.UserID = @UserID    
) t1
where rn = 1
ORDER BY ParentId, OrdinalPosition, Title
Now I have another table I need to join to my current select statement
SELECT [Customer]
      ,[Loyalty]
   
  FROM LCustomer where Customer  = @UserID

What I need to put together if loyalty = '0' then in the select appid 17 will not be selected.
if loyalty is 1 then select appid 17

how can i join what I have now with the select statment above. Not sure of the best method to accomplish this.
0
Comment
Question by:sevensnake77
3 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 450 total points
ID: 36543193
Add to your Where clause:
WHERE rn = 1
   and (loyalty = '1' or (loyalty = '0' and appid <> 17))
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 50 total points
ID: 36543274
try
select * from 
(
SELECT @UserID as userid,
         ga.AppPermission,
         a.Title,
         a.Url,
         a.Description,
         a.AppStatus,
         a.ParentId,
         a.Appid,
a.OrdinalPosition,
      row_number() over (partition by a.Title, a.Url, a.Description order by ParentId, OrdinalPosition, Title) rn
    FROM Users u
  JOIN UserGroup_new ug ON u.UserKey = ug.UserKey
  JOIN GroupApplications_test ga ON ug.GroupID = ga.GroupID
  JOIN (select a.* from site_SiteMap_beta a 
       where appid <> 17 
         or (appid = 17 and (
    SELECT [Loyalty]
      FROM LCustomer where Customer  = @UserID
         )=1)
      ) as a
  
    ON ga.AppID = a.AppID
WHERE u.UserID = @UserID     
) t1
where rn = 1
ORDER BY ParentId, OrdinalPosition, Title

Open in new window

0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 36545506
tks
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.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

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