?
Solved

Join tables

Posted on 2011-09-15
3
Medium Priority
?
299 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:Seven price
[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 27

Accepted Solution

by:
Shaun Kline earned 1800 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 200 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:Seven price
ID: 36545506
tks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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 setup several different housekeeping processes for a SQL Server.

719 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