SSRS - Using IF Condition in a Dataset - reporting-services

I have a table structure in place. But, the data I push into this data varies depending upon a input
Input A - Query X
Input B - Query Y
In order to achieve this, I was thinking, I will put in a IF Condition in the dataset and have it direct to "Query X" or "Query Y" based on the "Input".
I tried this,
IF(Input=='Hello')
(
...
)
But, this threw an error. Can you help me on how to proceed here? Is there a better way I can achieve my req.?

I'd advise against the IF statement for performance reasons, but if you must, the syntax in SQL is:
if ( 'blah' = 'blah' )
begin
--do stuff
end
Some other options you might consider, instead of an IF:
Use Stored Procedures, have a "master" sproc called by the report, which then executes the appropriate sproc based off the parameters it receives.
UNION ALL your potential queries together, and include a line in the WHERE clause to make only the query you want the results from return any rows.
If the differences in your queries are simple enough, perhaps you can just use CASE statements to apply the correct logic?

Related

SAP BO 4.2 SP07 inList clause in webi

Can i use another column in InList clause?
Example,
i have created a variable and below is the formula.
IF [query1.column1] inList ([query2.column2]) then SUM([query1.amountColumn])
Else 0
OR is it possible to put variable after inList in formula?
If not possible -- is there any other alternative to this?
I see two possible approaches. I will to use the eFashion universe for both solutions.
Solution #1
Here are my 2 queries to begin...
Run your queries. Click on the columns you want to compare, [query1].[column1] and [query2].[column2] in your case; [Query 1].[Month] and [Query 2].[Month] for me. Right-click and merge them. They must be dimensions and of the same data type.
Now create a variable based on [Query 2].[Month Name] which you can filter on to eliminate the results from Query 1 that do not match up to anything in Query 2.
[UV Month Name]=[Query 2].[Month Name]
The key here is you need to change the Qualification to "Detail" and set the Associated Dimension to what we just merged by clicking three dots to the right. Choose [Month Name] not from either query, but the merged dimension.
Now build out your table with whatever object you want from Query 1 and add in the variable we just created.
Now add a filter on that variable to only show row where it is not null.
And you are done.
Pros
Works when limiting query (query2) has a relatively large number of values (compare to Cons for Solution #2).
Cons
More complicated to set up
May run into universe or performance issues related to query being filtered (query1).
Solution #2
Building upon Solution #1, I duplicated Query 1 and renamed it Query 3. Now you can choose "Results from another query" to get the [query1].[column1] InList ([query2].[column2]) logic you want.
If you take this approach then you don't need to do the merge, variable, and filter. The results of the query are filter before being returned by the report.
Pros
Simple
Cons
The number of values coming from your second query must be relatively small. It varies by database or maybe even your universe. I have found if it is over 1,000 values I get an error when I run the query that it is "too complex".

SELECT WITH CALL PROCEDURE MySQL

I got a Stored Procedure names getStocks that have a parameter. The code below is a example how I want to do with the query. Is it possible with the SELECT QUERY we can call a procedure?
PS I didn't use Function cuz I'm getting loading problem when I apply it in populating DataGridview in my VB.Net
BEGIN
SELECT ItemId, CatalogNumber, call getStocks(ItemId) AS quantity,
Cost, Minimum, Maximum, TypeId, SupplierId FROM items;
END
You cant do this. CALL is its own statement that you cant mix with SELECT in any way. If you cant use a UDF, you have to preform the integration manually.
It makes sense that a CALL cant be used like this when you consider that a CALL can optionally output a resultset. It might return a resultset, it might now. It might have one cell, one row, one column, or many rows/columns. The columns are not known at call time so the optimizer couldn't validate any JOINs to it if you put a call in the FROM and the because it can produce more than one cell (or nothing), you cant reliably put it in the SELECT. Additionally, stored procedures can have output variables, which also doesnt make sence in the context of a SELECT statement.
In other words, because the output of procedures is probably incompatible with any part of a SELECT query, the makers of mysql globally prevent their mixing.
You want to use a User-Defined Function (UDF) : http://dev.mysql.com/doc/refman/5.7/en/create-function-udf.html . Unlike stored procedures, UDF's may be called inline in a query.

SQL Query on transformed table in SSIS

I have joined 5 tables and done transformation on these tables. Now I got a single table at the end. Now I want to perform sql query on this single table to filter records. But I don't know how to perform simple sql query on this table. I have attached a snap shot which shows the resulting table. How I get this resulting data set as the source? I want to populate my destination after filter out this data.
I am using SSIS 2008.
Click here to see the Table on which I want to perform a simple sql query
SELECT * FROM `first_table`
where `some_column` =
(
SELECT `*`
FROM second_table
WHERE
`some_column2`='something'
LIMIT 1
)
Try this code This will help. You can even use this to connect all those four tables with each other.
From the image you posted, it looks like you have a set of data in the dataflow you're trying to query against. You need to do one of two things at this point. Either you insert the data into a table in the database and use another data flow to query it, or you use use a conditional split (or multicast and conditional splits) to filter the rows down further from there.
Without more detail about what you're actually trying to accomplish, these are the recommendations I can determine.
You could send the rows into a record set destination, but you aren't able to query it like a regular table and you'd need some C#/VB skills to access it to do more than a FOR EACH loop.
Assuming your sql query that you want to run against the resulting table is simple, you can use a script component task. By simple, I mean, if it is of this nature:
SELECT * FROM T WHERE a = 'zz' and b = 'XX' etc.
However, if your query has self joins, then you would be better of dumping the outcome of joining those 5 tables in to a physical table, and go from there.
It appears that query is going to be real straight-forward; in that case using a script component would be helpful.
A separate question: It's advisable to do the sorting at the database level. You are using 5 sort tasks in your solution. Can you please elucidate the reason?

Using the result of an SP in an IN clause

I ran into a problem or maybe even a MySQL limitation.
The situation is as follows:
I have an SP X, selecting related records based on 1 or 2 arguments. The SP returns a list of 0, 1 or more id's. I wanna use that list in an IN clause like so:
SELECT
*
FROM
table
WHERE
id IN (spX(y));
This gives me an error:
Syntax error or access violation: 1305 FUNCTION z.spX does not exist
The error is a bit vague: I'm confident it's a syntax error rather than access violation or non-existence of the SP itself. If I CALL the SP directly I get my expected results.
This kinda feels like a dead end. My expectation was that MySQL would throw an error if the SP returned more than 1 column, not if it returned more than 1 row (in which case I could've used a FUNCTION instead and this would've worked straight away).
So, the question is: is there any way of using the SP's result in an IN clause?
On a side note: I'm aware I could achieve the same result by simply joining the table and then add a new where clause to the existing query, instead of using the SP. However, the real problem here is that new functionality has to be added to the application, and not having to join in tons of queries, but using a SP instead is the way of least resistance.
SELECT * FROM table WHERE id IN(SELECT spX FROM table WHERE y = ???);
you need a select statement in the parenthesis otherwise it is very unclear what you are doing.
If sPx is the name of your table and y is the variable you want to pull out of it, then you you need to say that with the select statement in parenthesis. Def just a syntax error
I implemented the solution as proposed in the chat:
https://chat.stackoverflow.com/rooms/12910/discussion-between-robin-v-g-and-hituptony
What I do step by step:
I put a placeholder in every query involved.
On a higher level in my application I call the stored procedure. To clarify: there're 2 things I know for sure when it comes to the SP:
It will always take 1 id as parameter.
It will always give 0 or more id's back.
I generate the IN() clause depending on the SP's outcome.
I replace the placeholder with the generated IN() clause and run my query.
This way I can always add or alter the conditions in the SP itself, without changing the application logic/queries.

Linq-to-Sql Count

I need to do a count on the items in a joined result set where a condition is true. I thus have a "from join where where" type of expression. This expression must end with a select or groupby. I do not need the column data actually and figure it is thus faster not to select it:
count = (from e in dc.entries select new {}).Count();
I have 2 questions:
Is there a faster way to do this in terms of the db load?
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields?
Thanks.
Please pay especial attention:
The query is a join and not a simple table thus I must use a select statement.
I will need 2 different query bodies because I do not need to load all the actual fields for the count but will for the list.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count. Look forward to those who understand what I'm asking for possible better ways to do this and some detailed knowledge of what actually happens. I need to pull out the logging to look into this deeper.
Queryable.Count
The query behavior that occurs as a
result of executing an expression tree
that represents calling
Count(IQueryable)
depends on the implementation of the
type of the source parameter. The
expected behavior is that it counts
the number of items in source.
In fact, if you use LinqToSql or LinqToEntities, Queryable.Count() is sent into the database. No columns are loaded to memory. Check the generated sql to confirm.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count
This is not true. Check the generated sql to confirm.
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields
If you need both the count and the list, get the list and count it.
If you need the count sometimes and other times you need the list... write a method that returns the complex IQueryable, and sometimes call .Count() and other times call .ToList();
I do not need the column data actually and figure it is thus faster not to select it.
This is basically false in your scenario. It can be true in a scenario where an index covers the result columns, but you don't have any result columns.
In your scenario, whatever index is chosen by the query optimizer, that index can be used to make the count.
Sum up: Query optimizer will perform the optimization you desire.
//you can put a where condition here
var queryEntries = from e in dc.entries select e;
//Get count
queryEntries.Count();
//Loop through Entries, so you basically returned all entries
foreach(entry en in queryEntries)
{}