Solved

Join tables

Posted on 2011-09-15
3
289 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

15 Experts available now in Live!

Get 1:1 Help Now