« Media Images can cause hair loss Main | Data Templates without grouping »

Joining Queries a la Francais

Bonjour mes ami!


I have just got back from a conference in the warmer climes of the French Riviera. Its been quite a shock; after nearly a week of mid 60s temperatures and blue seas coming back to somewhat chilly and mid 30s Colorado has taken some getting used to again. Thankfully the promised thaw is upon us and the piles of snow in my yard are now dwindling, much to my sons' chagrin the snowboard run we had in our back yard is becoming a distant memory. Ahh c'est la vie ... so to another article in the fragmented data template series.

I have spent some time looking at our documentation on data templates, its pretty good so please check it out. Im picking on a series of some of the tougher features to get your head around. for this article its creating and joining multiple queries.


There are two methods to join queries, lets assume we have the following queries:






SELECT DEPTNO,
DNAME,
LOC
FROM DEPT
ORDER BY DEPTNO
SELECT  DEPTNO,
EMPNO,
ENAME,
JOB,
MGR,
HIREDATE,
SAL,
NVL(COMM,0) COMM
FROM EMP


we want to create a data template that will use the DEPT query as the master and the EMP as the child. We need to link the two queries.


Method 1: Query Link

We can create a link similar to Oracle Reports, like this:

<link name="DEPTEMP_LINK" parentQuery="Q1" parentColumn="DEPTNO" childQuery="Q_2" childColumn="DEPTNO"/>


We name the link and then specify the queries and the linking columns, so the <dataQuery> section of our data template will look like:


<dataQuery>
 <sqlStatement name="Q1">
 <![CDATA[
    SELECT DEPTNO,DNAME,LOC from scott.dept
    order by deptno ]]>
 </sqlStatement>
 <sqlStatement name="Q2">
  <![CDATA[
     SELECT  DEPTNO, EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,nvl(COMM,0) COMM
     from scott.EMP ]]>
 </sqlStatement>
<link name="DEPTEMP_LINK" parentQuery="Q1" parentColumn="DEPTNO" childQuery="Q_2" childColumn="DEPTNO"/>
</dataQuery>


It works fine but we have to duplicate the DEPTNO column in the child query. Plus on a more real world query its going to be slower than taking ...

Method 2: Bind Variables


We did alot of testing on the joining of queries and found that usng bind variables was consistently faster then query links, so if you can, and there should not be a reason why you should not, use binds. What do I mena by bind variables, well taking our two queries we are able to have a master-detail relationship between the two i.e. the dept based query is the master and the emp the detail. We can join them quite easily using 'dept_id' as our joingin criteria:

<dataQuery>
 <sqlStatement name="Q1">
 <![CDATA[
    SELECT DEPTNUM,DNAME,LOC
from scott.dept
    order by deptno ]]>
 </sqlStatement>
 <sqlStatement name="Q2">
  <![CDATA[
     SELECT  DEPTNO, EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,nvl(COMM,0) COMM
     from scott.EMP
where DEPTNO = :DEPTNUM ]]>
 </sqlStatement>
</dataQuery>

Notice, I have changed the column names a little just to make the join clearer (dont cut 'n' paste this one.) We use :DEPTNUM from the first query in the where clause of the second, et voila we have two queries joined. Its simpler, less typing and even better its faster!
Salut!

Comments (6)

TIm,

Can you have more than 1 parent to child links in BI Publiher?


The Q2 childQuery has 21 rows and the Q3 childQuery has 22 rows when I run then by them self with the parent.

What happens when they are together is I get each Q2 child row dup'ed 21 times and each Q3 child row dup'ed 22 times the difference between Q2 and Q3 dup's are Q2 all the same row are together like 1,1,1.1,1... 2,2,2,2... and Q3 the rows are like this 1,2,3,..,1,2,3...,1,2,3.. 22 times.

What am I doing wrong? Also I need to add 4 more childQuery to this report so I will have 1 ParentQuery and 6 childQuery's.

Any ideas on how I can make this work.

Thanks
Kirk Haffer


Tim:

Hi Kirk
You should be able to do it no problem. Send me your data template or share it on the forum.
tim

Matt Shaver:

Tim,

I am having the same problems as Kirk where my result query gets duplicated when I have more than one link in a report.

Could you please share what the problem would be?

Tim:

Matt
Can you post your DT to the forum so we can see it?
Tim

Matt:

Tim,

I posted the template online just in case the blog might change some of the xml: http://www.trivalues.com/template.txt

The repetition happens when I add another link.

Shanak:

Hi,

I have a similar data structure. (multiple queries using bind variables)

query 1 executes fine. (info: query 1 does not use a bind variable)

query 2 do not return results (info: query 2 uses a value from query 1). when I checked the debug details, the bind variable is null.

any inputs?

Thanks

Post a comment