BIRT parameter dataSet - birt

I've into BIRT a query like this
SELECT a.1, a.2, a.3 FROM a WHERE a.4 = 1
then I write another query as
SELECT b.1, c.2, c.3 FROM b join C on b.1=c.1 WHERE b.2=?
I would use to parameter, instead of '?', all values of column a.1 from first query. It's possible in BIRT?

You could do this using joint datasets, as discussed in the answer to this question, but if both queries are on the same database then it would be easier to combine the two queries, like so:
SELECT b.1, c.2, c.3
FROM a
join b on a.1 = b.2
join C on b.1 = c.1
where a.4 = 1
Or if you only want distinct values of a.1, like this:
SELECT b.1, c.2, c.3
FROM b
join C on b.1 = c.1
where exists (select null from a where a.4 = 1 and a.1 = b.2)

Related

oracle - find record which is the last record

I have a query with more than one table with a lot of criteria that gives this result
select book, max(version)
from a,b
where condition 1, condition 2... and so on
the query is nice , the result is
book
version
book1
3
book2
2
book3
1
I want to join this result with another table c:
book
version
id
book1
1
id1
book1
2
id2
book1
3
id3 -> I want to join the result with this row
book1
4
id4
book2
1
id5
book2
2
id6 -> I want to join the result with this row
book3
1
id7 -> I want to join the result with this row
If you need to join Table A and Table C.
than try below query:
SELECT a.book, max(version) over
( partition by a.book order by a.book) version,c.id
FROM tablea a, tablec c
where a.book.c.book
and a.version=c.version;
Use a sub-query and join:
SELECT m.book,
c.version,
c.id
FROM (
select book,
max(version) AS max_version
from a INNER JOIN b
ON (condition 1)
where condition 2 ... and so one
) m
INNER JOIN c
ON ( m.book = c.book
AND m.max_version = version )

How to join hive tables based on condition of the joining column

We have a hive table like below:
num value
123 A
456 B
789 C
101 D
The joining table is:
num Symbols
123 ASC
456001 JEN
456002 JEN
456003 JEN
789001 CON
101 URB
Our expected result:
num value symbols
123 A ASC
456 B JEN
789 C CON
101 D URB
Currently we are joining the tables twice in order to get the results.
Like first time insert into some tmp table using the below query:
select
a.num,
a.value,
b.symbols
from mytable a
join mytable b on a.num = b.num;
This query is producing the results for keys 123,101.
Next, we are running another query like below:
select
a.num,
a.value,
b.symbols
from mytable a
join mytable b on CONCAT(a.num,'001') = b.num;
This query is producing the results for keys 456, 789.
These two queries results are inserted into some tmp hive table and we select the final results from the tmp table.
This looks a bad design overall. but I would like to know if there is a better way to achieve this. Thanks.
Query Result
for
Select
a.num
,a.value
,b.symbols
from
(select substr(num,3) as num, value from table)a
join
(select substr(num,3) as num, symbols from table) b
on a.num = b.num
a.num a.value b.symbols
3 A ASC
1 D URB
OK, just one sql can implement your requirement.see below, table a is the table with value column and table b is the table with the symbols column, the SQL:
select
distinct a.num,
a.value,
b.symbols
from
mytable1 a
join
mytable2 b on substr(cast(b.num as string),0,3) = cast(a.num as string)
If datatype of num is String then you can try with Substr
Select
a.num
,a.value
,b.symbols
from a join b on
substr(a.num,3) = substr(b.num,3)
Can you pls try this
Select
a.num
,a.value
,b.symbols
from
(select substr(num,3) as num, value from table)a
join
(select substr(num,3) as num, symbols from table) b
on a.num = b.num
Can you try with left semi join with above query as shown below.
Select
a.num,
a.value,
b.symbols
from
mytable1 a
Left semi join
mytable2 b on substr(cast(b.num as string),0,3) = cast(a.num as string)

How count table a data for every data of table a

I have three table A,B,C.
A table:
id,name
B table:
id,a_id,date
C table:
id,b_id,type(value is 0/1)
I want to print all A.name,A.id and C.countingdata by counting C data where C.type=1 using B table which has A table id
Result look like below:
A.id A.name C.countingdata
1 abc 4
2 vfd 2
3 fdg 0
Well, you can first inner join B and C, do the group by and get C.countingdata using count(). Another join on this subquery with B itself to accommodate the a_id
in the result set.
Now, you can do an inner join between A and the above subquery to get your results.
SQL:
select A.id, A.name, derived.countingData
from A
inner join (
select B.id as b_id,B.a_id,sub_data.countingData
from B
inner join (
select B.id,count(B.id) as countingData
from B
inner join C
on B.id = C.b_id
where C.type=1
group by B.id
) sub_data
on B.id = sub_data.id
) derived
on A.id = derived.a_id
You can find query as below:
Select
A.id
,A.name
,COUNT(C.id)
FROM A
JOIN B ON A.id = B.a_id
JOIN C ON B.id = C.b_id ANd C.type = 1
GROUP BY
A.id
,A.name

OUTER JOIN from different column names to same column

I am trying to select columns from different tables (the columns have different name) and use an outer join to get them in a single table. How do I do this?
(I am using sqlplus)
Here is an example:
Table a:
a.NAME1 a.NAME2 a.RATING
Jack Sparrow 4
Table b:
b.FIRSTNAME b.LASTNAME b.RATING
Jack Sparrow 7
Table 3:
c.F_NAME c.L_NAME c.RATING
Jack Sparrow 6
I would like a table like this:
NAME RATING
Jack 4
7
6
I tried this code
SELECT
a.NAME1 AS NAME,
b.FIRSTNAME AS NAME,
c.F_NAME AS NAME,
a.RATING AS RATING,
b.RATING AS RATING,
c.RATING AS RATING
FROM a
FULL OUTER JOIN (b
CROSS JOIN c)
ON (a.NAME1 = b.FIRSTNAME
AND a.NAME1 = c.F_NAME);
But that didn't work. How do I go about achieving this?
It does not sound like you want to join the tables at all. If you joined three tables each with 1 row, you would end up with a result set that had a single row and many columns. Since your goal is to end up with three rows of data, you would want to use a union all
SELECT a.name1, a.rating
FROM a
UNION ALL
SELECT b.firstname, b.rating
FROM b
UNION ALL
SELECT c.f_name, c.rating
FROM c
If you want to eliminate duplicate rows, use a union rather than a union all.
select a.NAME1, a.NAME2, a.RATING, b.RATING, c.RATING
from a
left outer join b on b.FIRSTNAME = a.NAME1 and b.LASTNAME = a.NAME2
left outer join c on c.F_NAME = a.NAME1 and c.L_NAME = a.NAME2

How to reduce the script lines for same table join condition in multiple times in oracle?

There are two tables table1 and table2.
Table1 is as below:
col1 | col2 | Col3
A 10 X
B 11 X
C 10 X
A 20 X
Table2 is as below:
col1 | col2 | col3 | col4
A 10 1 UDHAY
B 11 2 VIJAY
C 10 1 SURESH
A 20 2 ARUL
A 10 3 UDHAY
B 11 4 VIJAY
C 10 4 SURESH
A 20 5 ARUL
I want to display the column col4 in table2 with 3 join conditions as below.
table1.COl1 = table2.COl1
and table1.COl2 = table2.COl2
and table2.COl3 = '1'
Sample query :
select
table2.col4
from table1
left outer join table2
on(
table1.COl1 = table2.COl1
and table1.COl2 = table2.COl2
and table2.COl3 = '1');
Question: IF I want to display table2.col4 for condition table2.col3 1,2,3,4,5 with matching other condition from table1, how to make the script?
Actually I know we can add same table 5 times with different alias names and can print. But I don't want to repeat the same condition 5 times. Only the where conditions should be common for all 5 values.
Added on 30-OCT-2013:
Thanks for your response. NO not like you mentioned by using IN. Right now I am using below script concept :
select A.col1,A.co2,B1.col4 ,B2.col4,B3.col4.B4.col4
from table1 A
left outer join table2 B1
on(
A.COl1 = B1.COl1
and A.COl2 = B1.COl2
and B1.COl3 = '1')
left outer join table2 B2
on(
A.COl1 = B2.COl1
and A.COl2 = B2.COl2
and B2.COl3 = '2')
left outer join table2 B3
on(
A.COl1 = B3.COl1
and A.COl2 = B3.COl2
and B3.COl3 = '3')
left outer join table2 B4
on(
A.COl1 = B4.COl1
and A.COl2 = B4.COl2
and B4.COl3 = '4');
So my output will be:
A | 10 | UDHAY | |UDHAY| |
B | 11 | | VIJAY| | VIJAY |
C | 10 | SURESH | | | SURESH |
A | 20 | | ARUL | | |
But like above script I have to make it for 25 combination (1 to 25). So if I make the script like above the script lines will be more than 200 lines. To avoid that will you please help to suggest some other method to reduce the script lines and get the same output?
I'm not sure I'm getting you correctly, do you mean something like this?
and table2.COl3 IN ('1','2','3','4','5')
First solution (trivial):
Make a view with the whole set of Joins and query for it instead on your script, it's a bit dodgy but gets your script to be shorter. If your data set ends up growing off the scales you could switch to a materialized view instead and simplify what could end up being a costly plan.
Second solution (not so trivial):
If your tables do have a name convention you could write a PL block and loop through it, going around and arraying the tables that you want to join so that you can concat the joins on runtime in a string and run it as dynamic SQL.

Resources