BIRT : Data set with dynamic table name - birt

I'm new to Birt, so I might be doing this the wrong way...
What I need to do is to display some statistical data in reports.
Data will be prepared overnight in some special tables (I wanted to use materialized views, but haven't been able to find it in Birt report designer when connecting through JDBC to my DB - so I'll be using tables instead).
The typical chart I need to draw is :
- month on X-Axis
- a value on Y Axis.
For the Y Axis, there will be multiple series... But I don't know how many. Each serie data will be fetched from one of those special report table I was talking just above.
I have two main problems :
- how to display N series on a chart, N being unknown ?
- how to dynamize table names in a data set ?
Is there a more Birt-ish way to do so ? Maybe I have it completely wrong from the beginning, really don't know.
Thank you for your help.

Databases don't allow to dynamize table names with a regular query parameter, so we need to handle this by script.
The cleanest way would be to do this with a DB stored procedure: the BIRT dataset query just invokes a stored procedure of your DB with the table name as parameter. Here is an example of a such dataset query with MySql JDBC:
CALL `myschemaname`.`mystoredprocedure`(?);
Here, the question mark '?' is a query parameter, in the dataset editor we link it to a report parameter representing the table name. Then this stored procedure builds a dynamic query and returns relevant data.
Another option is to inject the table name in "beforeOpen" script of the BIRT dataset:
1- In your dataset, initialize the query with an arbitrary valid table name, for example:
select *
from myvalidtablename
2- In Eclipse designer select your dataset -> Script tab -> beforeOpen
Assuming the table name is in a report parameter:
this.queryText=this.queryText.replace("myvalidtablename",params["myTableNameParam"].value);
Concerning how to handle N series, it depends on how datarows are designed in tables. Two main cases:
Case 1 - Series are in multiple rows such:
month1;serie1;value
month1;serie2;value
month2;serie1;value
month2;serie2;value
This is the smartest case because it is dynamically handled in a BIRT chart with "Option Y series grouping". This is how this chart works: country field is set as optional grouping and BIRT handles everything.
Case 2 - Series are in multiple columns:
month1;valueSerie1;valueSerie2
month2;valueSerie1;valueSerie2
In this case we need to declare each serie explicitely. In the chart editor, by dropping down "Value (Y) series" menu we can create as many series as we like.

Related

How to create Interactive/Classic Report with dynamic SQL?

I'm running Apex 19.2 and I would like to create a classical or interactive report based on dynamic query.
The query I'm using is not known at design time. It depends on an page item value.
-- So I have a function that generates the SQL as follows
GetSQLQuery(:P1_MyItem);
This function may return something like
select Field1 from Table1
or
Select field1,field2 from Table1 inner join Table2 on ...
So it's not a sql query always with the same number of columns. It's completely variable.
I tried using PL/SQL function Body returning SQL Query but it seems like Apex needs to parse the query at design time.
Has anyone an idea how to solve that please ?
Cheers,
Thanks.
Enable the Use Generic Column Names option, as Koen said.
Then set Generic Column Count to the upper bound of the number of columns the query might return.
If you need dynamic column headers too, go to the region attributes and set Type (under Heading) to the appropriate value. PL/SQL Function Body is the most flexible and powerful option, but it's also the most work. Just make sure you return the correct number of headings as per the query.

Using PL/SQL block with an interactive grid (Oracle Apex)

How can I use a plsql code block like this with an Interactive Grid (Using Oracle Apex) :
begin
Query A;
exception when no_data_found then
Query B;
end;
Actually sometimes 'Query A' returns nothing and I want to run 'Query B'. any solution?
An interactive grid has to use a sql-query as source.
a. Write one query and use sql-query as source:
SELECT * FROM A
UNION ALL
SELECT * FROM B WHERE COUNT(SELECT * FROM A) = 0;
b. Write some function which does the work
Read this:
How to return a resultset / cursor from a Oracle PL/SQL anonymous block that executes Dynamic SQL?
But it sounds a little bit strange, that you got one grid for two datasources. This will bring up some problems when manipulating the data.
Open questions
Do you want to modify the data?
Do you want to insert new rows?
Does the user understand what's going on and what he is seeing?
Since there is no apparent way to NOT use an SQL query as the Interactive Grid source, you could maybe (depending on your specific solution) think differently and create an Interactive Grid region for each query. Then you could show one or another when the page loads, using a region server-side condition or even a Dynamic Action.
To expand on other answers with a little specificity, since this is about managing results of 2 different queries, you can put the 2 different queries in 2 different Grid regions. Then on the first region add a Server-side Condition of "Rows returned" and copy the SQL Query into the query input provided. On the 2nd region, you would set "No Rows returned" condition and again copy Query 1 into the SQL input provided.

Run 2 SETs in an Update Statement Oracle

Is it possible to update 2 columns in an Update statement that are in different tables? - The reason for the"scripted":
Where "Scripted" will be the "flag" so the formula does not run again on the same records if this field is filled in.
MERGE INTO arinvt_lot_docs ALD
USING
(SELECT arinvt.id,arinvt.class,fgmulti.in_date fgmulti.cuser3 FROM arinvt,fgmulti
WHERE arinvt.class LIKE 'CP%'
OR arinvt.class LIKE 'FG%'
OR arinvt.class LIKE 'IN%'
OR arinvt.class LIKE 'LA%'
OR arinvt.class LIKE 'PK%') Classes
ON (ALD.arinvt_id = classes.id
AND to_date(in_date) = '31-Dec-2015') --just picked a date to validate
WHEN MATCHED THEN
UPDATE SET non_conform_id = '21', fgmulti.cuser3 = 'SCRIPTED' --this text "Scripted" will fill in a field that will tell us in our reports if this was set by the script
I would like to join the tables using the arinvt.id field that is present in all 3 tables ARINVT_LOT_DOCS, FGMULTI & obviously ARINVT. ARINVT_LOT_DOCS & FGMULTI contain the NON_CONFROM_ID field that needs to be changed to '21'. The FGMULTI table also contains the CUSER3 field that would have "SCRIPTED" entered in it. The ARINVT table contains the Class of the inventory item which reflects in the conditions mentioned.
You cannot update two tables in one query in Oracle and other DBMS such as SQL Server but you can use transaction to achieve similar result.
This oracle community answers exactly that, if you try to join two tables, you will get this error
ORA-01776: cannot modify more than one base table through a join view
You can use transactions to update two tables in batch-like statement.
This https://stackoverflow.com/a/2044520 shows how to do it but for SQL Sever though. You need similar statement in Oracle.

Update Reports to filter with an Oracle function in Crystal Reports 2008

I have several reports, and on each one of them i have a few tables included.
Lets say 3 tables, tableA, tableB and tableC.
I want to use the following Oracle Function to filter the result set passing also a report parameter:
AND function(tableB.field1, tableB.field2, tableB.field3, {?report_parameter}) = 'S'
Facts to be aware of:
Oracle Functions can only be used on SQL Expressions and/or SQL Commands in Crystal Reports.
SQL Expressions cannot contain report parameters (so, not an option).
Crystal Reports lets you replace a report table with a SQL Command, but does not let you replace several tables to a single SQL Command.
We do not want to re-build all the reports all over again.
We do not want to replace a single table to a SQL Command because it affects performance in a high level since Crystal does not transform the tables and the SQL Command on a single query when executing the report.
This Oracle Function selects data from other tables and therefore can not be rewritten on a Report Custom Function.
The {?report_parameter} is an information that only the application knows. It is filled by the application before exporting the report to the user.
What could I do to work around this?
No knowing exactly why your function requires tables and parameter, I can only give you vague, untested options. None of the options are ideal:
rewrite your Oracle function as 'Custom Function' (CF); reference CF in record-selection formula; filter will be applied WhileReadingRecords (i.e. the function will not be sent to the database)
rewrite your function as a stored procedure that accepts one parameter, but returns a record set (the parameter will be recognized by Crystal Reports); join the other tables to the stored procedure. The stored procedure will need to be written in a manner that will support Crystal Reports' data needs (using result from a stored procedure with parameters as value for gauge chart - crystal reports in asp.net application)

Set query FROM table using report parameter in BIRT

I am using the BIRT report writer, and I have multiple tables with the same data structure. In my report design, I want to select the table my query uses as a report parameter (as part of a mysql query in the data set) -- but I can't figure it out.
When I create the data set, it's great that I can use parameters in the form of SELECT * FROM WHERE ?, and set these to report parameters, but I get an error if I set the ? to the table, such as:
SELECT * FROM ? WHERE 1
Is there another way I can do this? As it's java, I assume the syntax follows that of a PreparedStatement. In BIRT 2.5 there is a property binding option under the data set dialog... and I've tried setting my query as: "SELECT * FROM "+params["DataTable"].value+" WHERE 1", but that was also results in an error.
I just answered this same question over at BIRT Exchange: http://www.birt-exchange.org/forum/designing-birt-reports/19097-set-query-table-using-report-parameter.html#post62590
I should note that the queries MUST return the same fields & data types after you modify them. I only note this because of your statement above that the table structure varies "a bit". It is perfectly safe to modify the FROM or WHERE clauses in a BIRT query, but you cannot modify the actual SELECT construct.

Resources