Solved

Join tables

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this article I will describe the Backup & Restore 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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

9 Experts available now in Live!

Get 1:1 Help Now