Note that there are some explanatory texts on larger screens.

plurals
  1. POSSRS 2008 SQL statement help needed
    primarykey
    data
    text
    <p>I'm hoping someone can help me here. I have a report that will be run by either a single salesrep, a Regional Sales Manager (that has several salesreps that report to him) and "all over" managers that should be able to see ALL the salesreps. If the user is a salesrep, he should only be able to see the customers assigned to him; if the user is a Regional Manager, he should see his customers as well as all the other customers assigned to the reps that he manages; and the "all over" manager should be able to see every single salerep and all their customers.</p> <p>We have a salesrep table (SALESMAN_MSID) that has their salesrep #, their MSID value, their Regional Manager rep #, and the type of user they are -- "S" means they are a salesrep, "R" means they are a Regional Manager, and "M" means they are an all over manager. Here is the query I've got so far:</p> <pre><code>select distinct s.slsm_num, s.slsm_desc from salesman s where s.slsm_num in (select distinct slsm_num from salesman_msid where slsm_mgr_id = (@SlsmnRealNum)) or (s.slsm_num = (@SlsmnRealNum)) order by s.slsm_desc </code></pre> <p>This works great if the user is a salesrep or a Reg Mgr, but I can't figure out how to write the sql to get the all over results to work. The value that is in the parameter "@SlsmnRealNum" is their real salesrep #. </p> <p>The values in the SALESMAN_MSDI table are as such:</p> <pre><code>Slsm_num MSID Slsm_mgr_id User_Type ------------------------------------------------------ 1379 ljones 1316 S 1316 pcuday 1316 R 1401 mmay M </code></pre> <p>if I add in the clause "or user_type = 'M', I get ALL reps back regardless of if I'm testing as a S or a R.</p> <p>Can anyone help with the SQL statement to get the "M's" to return all but the other 2 to return as they should? </p> <p>ETA: here's the new sql statement (couldn't fit it in my comment??) -- thanks again to @Sklivvz! SELECT DISTINCT S.SLSM_NUM, S.SLSM_DESC FROM SALESMAN S WHERE S.SLSM_NUM IN (SELECT DISTINCT M.slsm_num FROM SALESMAN_MSID M WHERE M.slsm_num = (@SlsmnRealNum) --Themselves OR (m.slsm_mgr_id = (@SlsmnRealNum) AND EXISTS -- Their subordinates if type R (SELECT 1 FROM SALESMAN_MSID m WHERE m.slsm_num = (@SlsmnRealNum) AND m.User_Type = 'R')) OR EXISTS (SELECT 1 -- Everyone if type M FROM SALESMAN_MSID M WHERE m.slsm_num = (@SlsmnRealNum) AND m.USER_TYPE = 'M')) order by s.slsm_desc<code>enter code here</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload