a query

Dear Experts,

I am creating a dataset to show the user's job level. The field jobnumber has value from 1 to 6.  1 to 3 indicate user's job level is sales person, 4 to 6 is team lead, manger, and director. So, Users are in 4 to 6 should see everyone in 1 to 3. Users in  1 to 3 should be only able see himself or herself.

I created the following query:

-----------------------------------------------------------------------------------------
declare @UserID varchar (6)

;with list as      
      (
            select
                  UserName,
                  jobnumber,
                  (Case jobnumber
                        When 1 then 'Level 1'
                        when 2 then 'Level 2'
                        when 3 then 'Level 3'
                        Else 'Others'
                  End ) as JobLevel
            from
                  User
            where UserID =  @UserID

      )

select *
from list
---------------------------------------------------------------------------------------------------------------
This works perfectly for the user who is in 'Level 1' or 'Level 2' or 'Level 3'. They will only see themselves.

 what I should do to show all users who are in jobLevel of 'Level 1', 'Level  2', and 'Level 3')  when the user's jobLevel='Others'

Thank you very much
sharon2011Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
remove the Where condition within the CTE (where UserID =  @UserIDwhere UserID =  @UserID )
0
sharon2011Author Commented:
why?
0
sharon2011Author Commented:
then everyone see all users!
0
Aneesh RetnakaranDatabase AdministratorCommented:
declare @jobLevel sysname = 'others'

declare @UserID varchar (6)

;with list as      
      (
            select
                  UserName,
                  jobnumber,
                  (Case jobnumber
                        When 1 then 'Level 1'
                        when 2 then 'Level 2'
                        when 3 then 'Level 3'
                        Else 'Others'
                  End ) as JobLevel
            from
                  User
            where UserID =  @UserID

      )


select *
from list
where  jobLevel= Case when @jobLevel = 'others' then  jobLevel else @joblevel end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.