0

I found this question on SO elsewhere, but the answer included a part that doesn't pertain to me, so I must ask this question with my specifics.

I need to simply add an option in my dropdown menu. Here's my SQL query to be used for the salesperson only (debug mode):

declare @user varchar(30)
set @user = 'DOMAIN\ppelzel'

select SalesPerson_Name
from Salesperson
where salesperson_id =  case
    when  @user in ('DOMAIN\Brandyj',
'DOMAIN\jwilson','DOMAIN\KRoberts',
'DOMAIN\ppelzel','DOMAIN\bmurray')then salesperson_id
    else  SUBSTRING(@user,14,20)
    end 
order by 1

enter image description here Per my previous mention of another question like this asked, it said to not use a WHERE clause. I, however, must use a WHERE clause because I need it to determine if the person logged in matches what's in the dataset, then that is the only name they'll see, outside of a handful of 'Admin' users who need to see everyone.

For these same Admin users, I need to add an option to select all salespeople. I tried simply using the "allow multiple values" but it doesn't like that and gives me an error: Incorrect syntax near ','. even when I take out the WHERE clause in my query of sp.salesperson_name = @salesperson. Anyway, what's my best course of action for adding an All option for this report?

EDIT: I just realized I might need to add the main dataset query for context:

SELECT sp.SalesPerson_Name
    ,c.Calendar_Month_Name
    ,sum(isnull(sales_qty, 0)) AS 'total gallons'
    ,sum(isnull(Ext_Retail_Base, 0) + isnull(Ext_Retail_Freight, 0)) - sum(isnull(Ext_Cost_Base, 0) + isnull(Ext_Cost_Freight, 0)) 'Sales GM'
    ,(sum(isnull(Ext_Retail_Base, 0) + isnull(Ext_Retail_Freight, 0)) - sum(isnull(Ext_Cost_Base, 0) + isnull(Ext_Cost_Freight, 0))) / sum(isnull(sales_qty, 0)) 'cpg'
FROM Fuel_Wholesale_Sales_Fact fwsf
JOIN calendar c ON fwsf.Calendar_key = c.calendar_key
JOIN Salesperson sp ON sp.SalesPerson_Key = fwsf.Salesperson_Key
JOIN Customer cu ON fwsf.Customer_Key = cu.Customer_Key
WHERE  sp.SalesPerson_Name = @SalesPerson
    AND c.Day_Date BETWEEN @Start
        AND @End
    and isnull(fwsf.sales_qty,0) != 0
GROUP BY sp.SalesPerson_Name, c.Calendar_Month_Name

UPDATE 1: I attempted to use the STRING_SPLIT function, but even using the simple example from Microsoft's website (https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2016) resulted in me getting an error: Invalid object name 'STRING_SPLIT'. I am running SQL 2016. Thoughts?

Figured it out. Compatibility level issue. My DB is set on 110. I may need to ask some questions if there's a reason it's set on that as opposed to the default 130.


UPDATE 2: I finally sorted out what I needed. I just used the "Allow multiple values" option in the Parameter Properties. It wasn't working before because I foolishly did not use an IN operator nor surround the parameter with parentheses.

So I had the following: select salesperson where id = @salesperson

When what I needed was: select salesperson where id in (@salesperson) because Report Builder will pass a string of parameter values as 'Bob','Mary','John' which require that they be put inside the parentheses. This is for others who come looking for answers.

jw11432
  • 545
  • 2
  • 20
  • use a split function because in your main dataset query you are using sp.SalesPerson_Name = @SalesPerson; this should be something like sp.SalesPerson_Name in (select salesperson from dbo.splitfn @SalesPerson ) ... ofcouse your function will do heavy lifting to allow multiple values based on comma (most used) list or can be any other character you want. – junketsu Sep 26 '19 at 14:31
  • and since you will split function. You can change on the SSRS side to do multi-valued param. – junketsu Sep 26 '19 at 14:32
  • 1
    I suppose you are referring to STRING_SPLIT in T_SQL? I googled split function and that's what came back and based on quick preliminary reading, it seems to match. – jw11432 Sep 26 '19 at 14:45
  • you got it. there is code for it floating out there. But if you cant get that to work let me know and I can post something around it. – junketsu Sep 26 '19 at 15:00
  • 1
    Awesome, thank you! I'll start playing with it and post an update either way. – jw11432 Sep 26 '19 at 15:01
  • https://stackoverflow.com/questions/20512855/split-function-by-comma-in-sql-server-2008 – junketsu Sep 26 '19 at 15:05
  • few final thoughts you main data set query. 1) Make it into a PROC. So it runs faster. AND function will still be called from within proc. 2) Naming of params on SSRS IS case sensitive and not on sql server side unless you put that option on. – junketsu Sep 26 '19 at 15:21

0 Answers0