Apply a Report filter in code - ssrs-2008

My particular, specific issue is probably too localized, but the general question I'm about to ask is something I'm sure others will ask and have wondered:
General question: In Sql Server Reporting Services, is it possible to apply a filter to a report in code? If not, is it possible to use branching in the report filter based on the value of a variable, and can you point me to documentation or explain how to do it.
My specific example follows, to expand on what I mean by the above, in case I worded it badly:
I'm learning SSRS and the docs and Google are coming up short.
The desired effect is that we have a report based on an incident tracking system. In this system, we have various teams that can track incidents: IT Ops, Development, Security, etc. Each of these teams have team members assigned.
We have a base report that displays ALL incidents.
We have added a boolean parameter named "LimitByTeam", which produces a CheckBox on the report as you'd expect.
We have added a String parameter that accepts multiple values. The allowed values come from a data set that lists the teams. This has added the expected drop-down list to the report, allowing users to select one or more teams.
We added a dataset that contains team and team member.
If the CheckBox is NOT selected, we want to display all incidents. (The default)
If it IS selected, we want to have the report to filter based on the login ID of the person who created the incident ticket.
Were I to do this in SQL, I'd do it as
Select
(ticket fields)
From
Table
WHERE TicketCreator IN (
Select LoginId FROM TeamMembersTable
WHERE TeamName in ('IT Ops', 'Developers'))
In SQL, or in VB, etc, this would be simple.
In SSRS I'm not figuring out quite how to do this. I've gotten as far as figuring out that I can use Custom Code to do more complex logic using VB (and it appears to be VB.NET. HOORAY! Familiar territory)
So I've added custom code and verified that I can read the value of the report parameter, but I can't figure out for the life of me how to apply a filter if the parameter value is True. Here's what I've got.
Public Sub ApplyTeamFilter()
' Report.Parameters("LimitByTeam") is a
' boolean report parameter that I'm able to access
' so I've got the IF statement worked out
If Report.Parameters("LimitByTeam").Value = True Then
' Pseudo-code - I'm looking for something like Report.Filters.Add(filterstatement)
' Alternatively a way to change this to a function to return a list of items from
' the Team MembersTable table and use it in a custom expression.
End If
End Sub
The problem is that I can't seem to find a Filters property on the Report object, or any method that lets me find it. Since I couldn't find it there, I expanded my search to everything in this section of the MSDN library and can't find it, or anything even remotely resembling a way to do what I'm attempting.
I'm also trying to do something like this because I think I see a way to use this function:
Public Function IsLoginIdInTeam(ByVal LoginId as String, byVal Team As String) As Boolean
' Report.Parameters("LimitByTeam") is a
' boolean report parameter that I'm able to access
' so I've got the IF statement worked out
If Report.Parameters("LimitByTeam").Value = False Then
Return True ' Default
Else
' Access the TeamMembers table and look for a match
' Something like
' Convert.ToBoolean(Report.DataSets!TeamMembers.Compute(Count, "TeamName = '" & Team & "' AND LoginId = '" & LoginId & "'")
End If
End Function
But I can't figure out how to access the Data Sets in the report, either, much less the syntax for interacting with them. The pseudocode works with a System.Data.DataTable, but I suspect SSRS DataSets are a different beast.
Am I missing something blindingly obvious? Should I be giving up on filtering this way in the report, and try another track, like changing the original query in the DataSet?

I'm not a huge fan of boolean parameters in SSRS (or BIT in SQL for that matter.) You can't write things like
WHERE #MyBool OR #MyOtherBool
It needs to be
WHERE #MyBool = 1 or #MyOtherBool = 1
So if I have an SSRS report with a boolean called MyBoolParam and a multivalue text parameter called MyMultiSelectTextParam , a SQL query like this will work:
SELECT
MyField,
MyOtherField
FROM
MyTable t
WHERE
#MyBoolParam = 1
OR
t.MyField IN ( #MyMultiSelectTextParam)
(My preferred alternative to boolean parameters is actually a string with possible values, but it would be handled in the query in the same way.)
If you can't easily change your SQL query (such as using a third party SP) then you can also accomplish the same thing by adding a filter to the SSRS DataSet. Right click on the DataSet in the Report Data pane and look at the Dataset Properties. The Filters pane will let you add criteria that are evaluated for each row. I haven't used these as much with multivalue parameters, but the IN Operator should work for that.

Related

Deleting substrings throughout a column

I am trying to clean up throughout columns within a table to create a clear attribution/reference for reporting on my digital marketing campaigns. The goal is to keep one part of a string while deleting all others. All strings within my marketing campaigns have symbols separating each substring.
Attached are pictures of my current table and of the desired table.
I am essentially trying to only keep on part of the structure of a string and delete all other sub strings. I have already managed to do this successfully by applying the following formula given to be from a separate thread.
update adwords
set campaign = substring(campaign from '%-%-#"%#"' for '#')
where campaign like '%-%-%';
This worked perfectly, however, I do not fully understand why and have not found a clear answer thus far on this forum.
How would I apply this to future rows? Ad group and match type can be used for this purpose.
Many Thanks.
First thing: You do not modify source data. Do ETL instead, and transform it to a final stage. Do that periodically and thus taking care of new data.
You could just create a trigger which should work for all new data, but there are 2 caveats with that:
Failure will lead to missing data and you not being able to QA it.
If you modify the source data in an incorrect way by mistake, you cannot undo it unless you have a backup, and even then it's just too hard.
So instead look at ETL tools like Talend or Pentaho Kettle; create your own ETL scripts, or whatever. Use Jenkins to schedule all of this periodically and you're set.
Now, about the transformation itself.
for '#'
indicates that # will be an escape symbol, which means that #" will be treated as a regular quote in this case.
substring(campaign from '%-%-#"%#"' for '#')
thus, selects everything between the quotes in the pattern. % is a wildcard, same as used in LIKE comparisons. So everything in the last group will be returned. This can better be done with regular expressions
substring(campaign from '.*?-.*?-(.*)')
For the second column the regex would be ^(.*?)\s*\{
And for the third one - similar: ^(.*?)\s*\}
I would create the new table like this:
CREATE TABLE aw_final AS
SELECT
substring(campaign FROM '^\w{2}-\w+-(.*)$') AS campaign,
substring(ad_group FROM '^(\w+)\s*\{\w+\}$') AS ad_group,
substring(match_type FROM '^(\w+)\s*\}$') AS match_type
FROM adwords
WHERE campaign ~ '^\w{2}-\w+-(.*)$'
But if you must do an update, this would be how:
UPDATE adwords SET
campaign = substring(campaign FROM '^\w{2}-\w+-(.*)$'),
ad_group = substring(ad_group FROM '^(\w+)\s*\{\w+\}$'),
match_type = substring(match_type FROM '^(\w+)\s*\}$')
WHERE campaign ~ '^\w{2}-\w+-(.*)$'

Prioritise which identifier to use

My crystal report pulls data about books, including an identifier (isbn, issn order number etc.), author, and publisher.
The ID field stores multiple ways to identify the book. The report displays any of the identifiers for that record. If one book has two identifiers; issn and order number, the report currently displays one apparently at random.
How can I make it prioritise which type to use based on a preset order? I figured some sort of filter on the field could work, but I haven't figured out how. I can't edit the table, but I can use SQL within the report.
If all the different types of ID are stored in a single field, your best bet is to use a SQL Command inside your report to separate them into multiple virtual fields.
Go to Database Fields / Database Expert, expand the connection you want to use, and pick Add Command. From here you can write a custom SQL statement to grab the information you're currently using, and at the same time separate the ID field into multiple different fields (as far as the report will be concerned, anyway. The table will stay unchanged.)
The trick is to figure out how to write your command to do the separation. We don't know what your data looks like, so you're on your own from here.
Based on the very little information that you have provided and if i was to make a guess.I suggest you make use of the formula field in your report and then use something like this to accomplish your goal.
IF ISNULL{first_priority_field_name} OR {first_priority_field_name} = '' THEN
{second_priority_field_name}
ELSE
{first_priority_field_name}
Use nested IF statement in case there are more than 2 identifier fields.

Microsoft Access - Creating a form which looks up data between 2 dates

For my College assignment, I have to create a database in Access, I have done 99% of my database, apart from this section which I'm stuck on.
In my DB, I have a tickets table, which contains records on order information and a field containing a date. For my assignment, I have to create a Form which reads from a Query.
For example, in my Form i have already created i have 2 Combo boxes with the dates already pulled from the Query. I need to be able to drop down one of the boxes and input 1 date, and then drop down the other box and select a different date, press a button and it generate me a Report.
The part I am asking for help on is the expression which is used to look up the data inside the Query. I tried using this expression, which Access said was too complicated.
[Forms]![frmOrdersBetweenTwoDates]![Combo33] And [Forms]![frmOrdersBetweenTwoDates]![Combo36]
My full SQL query is:
SELECT tblTickets.CustomerID, tblCustomers.FullName, tblCustomers.AddressLine1, tblTickets.OrderNumber, tblTickets.OrderDate
FROM tblCustomers INNER JOIN tblTickets ON tblCustomers.[CustomerID] = tblTickets.[CustomerID]
WHERE ((("WHERE [OrderDate]") Between [Forms]![frmOrdersBetweenTwoDates]![Combo52] And [Forms]![frmOrdersBetweenTwoDates]![Combo54]));
My expression/query now returns the report, but there is no data inside the report. How could i fix this?
Cheers.
Should post the complete query statement. Expect the filter clause should be like:
WHERE [date fieldname] BETWEEN [Forms]![frmOrdersBetweenTwoDates]![Combo33] AND [Forms]![frmOrdersBetweenTwoDates]![Combo36]
However, I don't use dynamic parameterized queries. I prefer to use the WHERE argument of OpenReport (same for OpenForm), in VBA:
DoCmd.OpenReport "report name", , , "[date fieldname] BETWEEN #" & Me.Combo33 & "# AND #" & Me.Combo36 & "#"

Select All parameter doesn't work in SSRS

I have a report which creates a list of Events for a specified date range and event type.
The date range and event type are parameters defined in the report. The date parameters (#DateFrom and #DateTo) work as they should.
The #EventType parameter however, which is defined as a list of values provided by a DataSet (with 'Allow Multiple values' checked), does not provide the expected behaviour when using the {Select All} check box. If I select one or more Event Types by checking several boxes on the list, the report will show the Events which match the specified Event Types correctly.
However, if I click the {Select All} box (which then highlights all of the other possible values), the report does not show the Events for all of these Event Type values. It seems to miss out several of the values which are selected by the {Select All} box. If I run the report specifically for those missing values, the report returns events matching those types. This indicates to me that there is not a lack of data for these types.
And for that reason, it looks to me like the {Select All} is bugged...or perhaps cached somewhere? I've tried deleting the report/parameter dataset and redeploying to no avail. It's worth noting that this behaviour happens locally before deploying it, too.
Has anyone seen this before, or does anyone have any suggestions?
EDIT - I should also mention that the parameter in question (#EventType) has no default value assigned.
How are you declaring your predicate for the variable? It should be be like:
where thing in (#Variable)
Where #Variable is a multi value parameter.
You could attempt to see if the values of the multi valued parameters are junked up somewhere as well by defining them. Generally the collection method of multi valued parameters can cause issues if there data types are different.
Also you may try to design your data set at runtime to build instead of being a static query. EG: Set up an expression for your dataset like:
="Select * from table where thing in (" & Parameters!Variable.Value & ")"
This would cause the parameter to build as part of a string and then evaluate at run time instead of from a traditional query.
Can't quite believe that this was the case, but the parameter which was passed to the SQL Server procedure was too small. It was defined as a VARCHAR(500) and needed to be bigger to deal with a large list of comma separated values. I changed it to VARCHAR(4000) and it's now functioning as expected.
Thanks to Djangojazz for pointing me to the direction of the parameter.

APEX - Can a Tabular Form with its MRU functionality have filtering functionality like an Interactive Report?

What I really need is a Tabular form that allows me to update multiple rows at a time while being filterable like an Interactive report. Any chance this is even remotely possible?
I would also like to hijack the row checkboxes on a tabular form that appear when the 'delete' functionality is activated and use them to select which rows get assigned a to a group based on a common attribute. (i.e. My table contains parts in my inventory and I need to be able to assign parts with common attributes to a group for processing)
Perhaps a group-by function that creates a new row in a 'Group' table with the group as the PK and the parts assigned to that group as a list or something...?
Thoughts? I am kind of at a loss...
It's really not that hard :) You can easily transform an IR into a pseudo-tabular form. And even though there are always more stylish and elegant solutions, those usually involve lots of javascript and/or plugins. They're nice, but not always what you want or need of course.
So how to manipulate your output? Use the APEX_ITEM api!
Quick example. I have an ir built on emp. I added a checkbox and textbox.
select empno, ename, deptno,
apex_item.checkbox(1, empno) empno_selected,
apex_item.text(2, ename, 10, 10) ename_edit
from emp
Don't forget: in the column attributes, set Display text as to Standard Report Column. If you don't, the output will be in plain text. Since apex_item generates html code, you don't want the plain text of course :)
Now, to be able to perform DML based on the actions you do in those generated fields, you will need a process. Let me start off by pointing out though that the generated items are stored in application variables, namely in arrays in APEX_APPLICATION.
Take note of the behaviour of checkboxes: only ticked boxes will have their value stored in the array!
As an example, i've made this small On Submit process (also adding a SUBMIT button on the form to actually perform the submit...)
for i in 1..apex_application.g_f01.count
loop
insert into empselected(empno, selectiondate, ename_changed)
values(apex_application.g_f01(i), sysdate, apex_application.g_f02(i));
end loop;
This will loop over the records with the checkboxes ticked, and insert them into some table. For example, i ticked the box with KING and edited the textfield. See record nr 2 (1 is from a previous action ;))
It's not all the way there yet though. You still miss out on the functionality of a tabular form and it's processes, and things like optimistic locking. If you want to stay with tabular forms, you can also, for example, check out this link. There have also been some questions here about writing your own mru processes etc, like this one ;)
It is possisble, using the same tabular form.
Create an item (text item) in the tabular form region.
Create a submit button (Create a button displayed among this region's items)
Modify the where clause in the tabular form region source
For Example, you need search by customer name:
WHERE lower(CUSTOMER_NAME) LIKE '%'||lower(nvl(:PXX_SEARCH,CUSOTOMER_NAME))||'%'
If you need to search for other field only add other condition to the where clause
WHERE (
lower(CUSTOMER_NAME) LIKE '%'||lower(nvl(:PXX_SEARCH,CUSOTOMER_NAME))||'%'
or lower(CUSTOMER_address) LIKE '%'||lower(nvl(:PXX_SEARCH,CUSOTOMER_NAME))||'%'
)
Simple and use the same tabular form.