Solved

Join tables

Posted on 2011-09-15
3
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

738 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