Users want to have an option to control which columns are present in the final report.
One option is to get a list of fields requested. Is it possible in SQL Server to write a query like this? But I need to select a list of values, not just a single variable.
My plan is to get a parameter's list from a query like this:
SELECT
CONCAT ('Table2.', name) AS ColumnName
FROM
sys.columns
WHERE
object_id = OBJECT_ID('Table1')
The user would select columns they need this time, and then I want to use parameter list somehow like that:
SELECT #ColumnName FROM Table2
Of course it's not working this way...
Is there an option to get result that I want?
Check 'allow multiple values' for your parameter
In hide/show expression of the visibility of the column, put:
=IIF(instr(join(Parameters!ParamName.label,","),"NameColumn_to_filter")>0,false,true)
Related
I'm using Report Builder and want to add more than one value against a Label (Available Values > Specify Values):
Example:
Label = "labelone" Value = Value1, Value2...
When I run the report, I get a message saying Parameter1 is missing.
I have tried following against value: Value1, Value2 and 'Value1', 'Value2'
My query has following in the where clause: WHERE (FIELD in (#Parameter1))
Basically, I want to specify multiple values against one label under the parameter.
Thank you
You cannot do this directly as what will get passed to the query would be
WHERE Field IN ('Value1, Value2')
or
WHERE Field IN ('''Value1'', ''Value2''')
The way to do this would be split the label using a table valued function, you could use string_split if you have a fairly recent version of SQL Server (I think this was introduced in SQL2016). If you have an older version there are loads of sample of string splitting functions around tha you could use.
Once you have you function you can join to the results of that, the query would be something like
DECLARE #pValues TABLE (myValues varchar(10))
INSERT INTO #pValues
SELECT * FROM string_split(#myReportParameter, ',')
SELECT *
FROM myTable a
JOIN #pValues p on a.Field = p.myValues
I have been trying for some time to get a Parameter to work within an MDX Query which is automatically generated by SSRS Query Designer.
I have tries the following:
FROM ( SELECT ( {[Dim Date].[Fiscal Year].&[+"#Current_FYear"+]}) ON COLUMNS
FROM ( SELECT ( { [Dim Date].[Fiscal Year].[+"#Current_FYear"+} ) ON COLUMNS
FROM ( SELECT ( {StrToMember(#Current_FYear,CONSTRAINED).lag(23):StrToMember(#Current_FYear, CONSTRAINED)}) ON COLUMNS
FROM ( SELECT ( {[Dim Date].[Fiscal Year].[+"Parameters!Current_FYear.Value"+]}) ON COLUMNS
And none of the above give me the result i am looking for: To have the Dataset filter by the value in the Parameter.
Any help on this would b emuch appreciated!
Thanks,
First of all, I would suggest using the Parameter checkbox in the Query Designer and let SSRS build the MDX for you. Here's a screenshot in case you missed it.
Another reason to let the report create the parameter for you is that it will automatically create another hidden Dataset to populate the available values of the parameter.
If you do want to edit the MDX manually, the syntax could look like this:
SELECT NON EMPTY { [Measures].[Measure] } ON COLUMNS FROM (
SELECT ( STRTOSET(#Parameter, CONSTRAINED) ) ON COLUMNS FROM [Model])
The STRTOSET function converts the parameter value into MDX syntax. It can also handle multi-value parameters.
The parameter you reference here would also need to be defined in the Parameters tab of the Dataset Properties. This is where you link the parameter value from the report to the query.
Their are 2 different jobstatuses that I want to display the same way, my IIF statement changes the display, BUT I would like for only one entry to be returned for both. For example, I want it to show like this
JobStatus
---------
Inactive
Let Go
But the returned result set I get is
JobStatus
---------
Active
Inactive
Let Go
Let Go
What do I need to change in my query to make it show like I need? (and I need this to be done via a query not vba)
jobstatus1: IIf([jobstatus]="Terminated","Let Go",IIf([jobstatus]="Fired","Let Go",[jobstatus]))
EDIT -- Full Syntax. The IIF statement I need to use is for the alpha.jobstatus line right after the Select
SELECT alpha.jobstatus, Count(beta.ID) AS CountOfID
FROM alpha LEFT JOIN beta ON alpha.jobstatus = beta.jobstatus
GROUP BY alpha.jobstatus, alpha.order
HAVING (((alpha.jobstatus) Not In (Select [jobstatus] From tbl_Valid)))
ORDER BY alpha.order;
Use SELECT DISTINCT to return only distinct values.
Also you can use a simpler expression for that calculated field --- one with a single IIf instead of nested IIf's. Switch your query to SQL View and revise the start of the statement text to this ...
SELECT DISTINCT IIf([jobstatus] IN ('Terminated', 'Fired'), 'Let Go', [jobstatus]) AS jobstatus1
Im trying to write a query that will pull the value of the selected item in a combobox to use in a WHERE clause.
For example, the user selects "Jeff" out the combobox that is named cboNames. The bound value is 1 (the primary key). I'm trying to create the query:
select * from tbleEmployees where empID = cboNames.Value
obviously this doesn't work. Get what I'm trying to do?
How do i place the control value into the query??
select * from tbleEmployees where empID = [Forms]![YourForm]![cboNames]
I'm trying to get column names from a table. I want to supply the row id and I want only the column names for which the value of that column for the specific row (identified by the id) is "true" (my table has a bunch of boolean fields).
I want something like:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME.value = true
AND TABLE_THE_COLUMN_IS_FROM.id = "some_id"
Where .value would be variable, basically checking each column to see if it were true.
I know I can just get the row's values and iterate through, returning only those with value true, but I wanted to see if there is a way to do it all in one step. Thanks in advance to anyone who knows!
There is no means in one query to dynamically scan through the table's schema and inspect its values. The best way to achieve what you want is the one you suggested: query for the row client-side and then cycle through the columns searching for the values you seek. The other alternative is to query for the table schema using the INFORMATION SCHEMA views client-side, build a SQL statement with a where clause that looks for a True value in all the boolean columns, execute that and inspect the results.
This is probably going to be rather ugly no matter how you cut it, but here's one option:
select columns.column_name
from bool_table
inner join information_schema.columns
on columns.table_schema = 'your_db'
and columns.table_name = 'bool_table'
and ((columns.column_name = 'bool_1' and bool_table.bool_1 = 1)
or (columns.column_name = 'bool_2' and bool_table.bool_2 = 1))
where bool_table.id = 25
You could also potentially query information_schema.columns to dynamically generate the list of column statements so that you could dynamically generate the query, and even execute it in a stored proc using dynamic sql in mysql.