SRSS Report Builder SUM IIF from different datasets - sql

I have two datasets in Report Builder 3.0 with a similar field and I want to put a SUM of the number occurrences of a particular value in that common field across both datasets.
I've got an expression I'm using for each individual dataset:
=SUM(IIF(Fields!caseorigin.Value = "mail",1,0))
and
=SUM(IIF(Fields!cliorigin.Value = "mail",1,0))
But I can't seem to work out a way to sum the values from both datasets. I've tried:
=SUM(IIF((Fields!caseorigin.Value, "caseDS") = "mail",1,0)) + SUM(IIF((Fields!cliorigin.Value, "cliDS") = "mail",1,0))
Is there any way to make this work, or an alternative method?

Just looks like a syntax error here; when specifying a scope it should like something like:
=Sum(Expression, Scope)
Applying this to your example:
=SUM(IIF(Fields!caseorigin.Value = "mail",1,0), "caseDS")
+ SUM(IIF(Fields!cliorigin.Value = "mail",1,0), "cliDS")
Should work for you.

Related

Can't Access a global view table with SQL by a selected field code converted from a dropdown selection

I am using a SQL notebook in Databricks/Spark community edition
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = "2YD"')
display(education_DF)
The above code cell works fine and retrieves the desired rows from the view.
However the following code cell gives an error.
I am supbstituting the hard-coded value "2YD" in the WHERE clause, with a variable call education_choice that contains the same value.
It seems that I am not using variables correctly in SQL. How would I make this work?
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = education_choice')
display(education_DF)
(This is the error)
org.apache.spark.sql.AnalysisException: cannot resolve '`education_choice`' given input columns: [global_temp.population_globaltmp_view.Salary, global_temp.population_globaltmp_view.Address, global_temp.population_globaltmp_view.Race, global_temp.population_globaltmp_view.MiddleI, global_temp.population_globaltmp_view.Education, global_temp.population_globaltmp_view.HairColor, global_temp.population_globaltmp_view.Age, global_temp.population_globaltmp_view.FullName, global_temp.population_globaltmp_view.City, global_temp.population_globaltmp_view.FirstName, global_temp.population_globaltmp_view.State, global_temp.population_globaltmp_view.LastName, global_temp.population_globaltmp_view.Height, global_temp.population_globaltmp_view.Fertility, global_temp.population_globaltmp_view.Employment, global_temp.population_globaltmp_view.Zip, global_temp.population_globaltmp_view.Weight, global_temp.population_globaltmp_view.Gender]; line 1 pos 72;
I tried the injection method suggested, but got a slightly different error this time. I included a screen shot of the relevant code cells. It seems that the education_choice "2YD" isn't being recognized as a literal but a field or something like that.
Shouldn't 2YD have quotes around it like "2YD"
If I hard code the WHERE clause like this: WHERE Education = "2YD" the queries works fine.
The image bellow is small but if you right click on it and "open in new tap, it is very readable.
*EDIT
You use the variable education_choice as a hardcoded string. Instead, you should inject the value of education_choice inside the SQL expression string like that.
*edit - wrap the variable with quoets.
%python
education_DF = sqlContext.sql('select * from global_temp.population_Globaltmp_view where `Education` = "{}"'.format(education_choice))
display(education_DF)

How to send a table names as parameters to a function which performs a join on them?

Currently I used the following code for joining tables.
Booking.joins(:table1, :table2, :table3, :table4).other_queries
However, the number of tables to be joined with depends on certain conditions. The other_queries also form a very large chain. So, I am duplicating a lot of code just because I need to perform joins differently.
So, I want to implement something like this
def method(params)
Booking.joins(params).other_queries
end
How can this be done?
Maybe just Booking.joins(*params).other_queries is what you need?
Operator * transforms array into list of params, for example:
arr = [1,2,3]
any_method(*arr) # is equal to any_method(1,2,3)
However, if params is smth came from user I recommend you not to trust it, it probably could be security issue. But if you trust it or filter it - why not.
SAFE_JOINS = [:table1, :table2, :table3]
def method(params)
booking = Booking.scoped # or Booking.all if you are rails 5
(params[:joins] & SAFE_JOINS.map(&:to_s)).each do |j|
booking = booking.joins(j.intern)
end
end

Using a Join query with ignited datatables trying to get a where with two conditions

I am attempting to pull data from mysql database using codeigniter and ignited datatables. I have no troubles pulling the join query with a single where clause, but when I try to add a WHERE x OR y, I can't seem to get it working. Here is the basic code that works fine:
$table = 'test_base';
$table2 = 'lab';
$this->datatables->select('test_base.idlab');
$this->datatables->from($table);
$this->datatables->join($table2, 'test_base.idlab = lab.idlab');
$this->datatables->where('lab.idaccount',$idaccount);
If I wanted to put multiple conditions in the query, I see from the manual that I can put multiple conditions in an array, but this seems to only do an AND, not an OR statement.
I then see that I may be able to create my own sql query using the following:
$this->datatables->where('column != "string"');
So, I tried this:
$this->datatables->where("`lab`.`idaccount`= $idaccount OR `lab`.`idlab` = 0");
SELECT `test_base`.`idlab`
FROM (`test_base`)
JOIN `lab` ON `test_base`.`idlab` = `lab`.`idlab`
WHERE `lab`.`idaccount`=` 124 OR `lab`.`idlab` = 0
ORDER BY `idtest` asc
The issue is that there is an extra (`) in the WHERE `lab`.`idaccount` = `<-here and I'm not sure how to get rid of it.
As #Ehecatl suggests, I got it working by entering the full query into the (). There were no examples on the CodeIgniter website or anywhere else I could find. Still new to CodeIgniter so maybe this will also help others.
Resolution is to put the query string in the $this->datatables->where(); and removing all the single quotes.
$this->datatables->where("lab.idaccount = $idaccount OR lab.idlab = 0");

Search for a Dataset where a associated Dataset has a specific value using Active-Record?

i am currently trying to do a "complex" search inside my Datasets.
I want to find every Dataset that is associated with a specific tool.
I tried something like this:
Dataset.find_by_sql("SELECT * FROM datasets, tools, sectors, products WHERE datasets.id = sectors.dataset_id AND sectors.product_id = products.id AND products.tool_id = tools.id AND tools.name = '#{toolname}'")
This works well. But we want to define scopes for querys, for being able to combine some of them later like: Dataset.find_by_toolname("foo").find_by_sector_name("foo2"). So i defined a scope for this:
scope :with_tool_name, ->(toolname) {find_by_sql("SELECT * FROM datasets, tools, sectors, products WHERE datasets.id = sectors.dataset_id AND sectors.product_id = products.id AND products.tool_id = tools.id AND tools.name = '#{toolname}'")}
The Problem here is that this query will return an Array and not an ActiveRecord-Relation so that i a am not able to do another scope-request on the result of the first.
So how can i fetch for Data where an attribute of some associated model has a specific value .. and then apply another scope on this ?
Here is what the Data-Model looks like:
You can use the activerecord-method joins(). And the result of every join can be mixed together with the method merge(). Here a small example to get all datasets for one tool_id.
Dataset.joins(:sectors).merge(Sector.joins(:product)).merge(Product.joins(:tool).where(:tool_id => 1))

How show conditional sum using multiple dataset?

I need show on my reportviewer the conditional sum getting data of other dataset, because my report show data of multiples dataset.
=Sum(iif(Fields!Id.Value = Parameters!Id.Value, Fields!Jan.Value,""),"Other_DataSet")
This code below works fine. But use default dataset, setted on bind propertie.
=Sum(iif(Fields!Id.Value = Parameters!Id.Value, Fields!Jan.Value,""))
This would be solved by using the Lookup function.
=SUM(IIF(Fields!Id.Value = Parameters!Jan.Value, LOOKUP(Fields!Id.Value, Fields!MatchingKeyIn2ndDataset.Value, Fields!ValueToReturn.Value, "Other_Dataset"), 0)