custom SQL in ODBC

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

custom SQL in ODBC

Post by dr46014 »

i have two tables cbfact and attr.there are 3 columns in cb fact: id,monthcode,pqr and 4 columns in attr:id,weekcode,abc,xyz.i want to have a left outer join between the two tables on the basis of latest partition.latest partition means from mothcode current monthcode data will be compared with current weekcode data of attr.monthcode comes in this fashion 200701,200702...200712 nad weekcode comes in the fashion 200701,200702,...200750,200751,200752 like this.
if in the odbc stage i am writing the below query it is showing error message..
can u suggest me some sql.i want to do it through database level not in job level

select a.ID,a.mthcode,b.abc,b.xyz
from
(
select * from cbfact where mthcode in (max(mthcode) from cbfact
) a
and
(
select * from cbfact where weekcode in (max(weekcode) from attr
) b
where
a.ID=b.ID(+)
us1aslam1us
Charter Member
Charter Member
Posts: 822
Joined: Sat Sep 17, 2005 5:25 pm
Location: USA

Re: custom SQL in ODBC

Post by us1aslam1us »

dr46014 wrote: select a.ID,a.mthcode,b.abc,b.xyz
from
(
select * from cbfact where mthcode in (max(mthcode) from cbfact
) a
and
(
select * from cbfact where weekcode in (max(weekcode) from attr
) b
where
a.ID=b.ID(+)
You need to do something like this:

Code: Select all

select a.ID,a.mthcode,b.abc,b.xyz 
from 
cbfact a, attr b
where 
a.ID=b.ID(+)
Then specify your mthcode and weekcode logic in the "where" clause.
I haven't failed, I've found 10,000 ways that don't work.
Thomas Alva Edison(1847-1931)
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

i have tried this sql but its giving incorrect records..
the problem here is that from cb fact we need to find the latest month code data and from attr we need to find the latest week data.and perform left outer join.
us1aslam1us
Charter Member
Charter Member
Posts: 822
Joined: Sat Sep 17, 2005 5:25 pm
Location: USA

Post by us1aslam1us »

what exactly you mean by latest data for month and code? Send some sample data.
I haven't failed, I've found 10,000 ways that don't work.
Thomas Alva Edison(1847-1931)
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

this is the sample data what i want
cbfact attr

id monthcode pqr id weekcode abc xyz
aa 200606 1234 aa 200625 1256 2563
bb 200606 4563 bb 200625 1563 1563
cc 200606 4589 cc 200625 1256 2563
dd 200606 4568 dd 200624 7865 4569
ee 200605 4189



output

id montcode weekcode abc xyz
aa 200606 200625 1256 2563
bb 200606 200625 1563 1563
cc 200606 200625 1256 2563
dd 200606 null null null
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

cbfact | attr
*********** *********

id monthcode pqr id weekcode abc xyz
aa 200606 1234 | aa 200625 1256 2563
bb 200606 4563 | bb 200625 1563 1563
cc 200606 4589 | cc 200625 1256 2563
dd 200606 4568 dd 200624 7865 4569
ee 200605 4189 |



output

id montcode weekcode abc xyz
aa 200606 200625 1256 2563
bb 200606 200625 1563 1563
cc 200606 200625 1256 2563
dd 200606 null null null
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

CREATE TABLE TAB1 AS select a.ID,a.MTHCODE
FROM CBFACT a
where
a.mthcode in(select max(mthcode) FROM CBFACT)



CREATE TABLE TAB3 AS
SELECT b.ID,b.ABC,b.XYZ,b.weekcode
FROM ATTR b
WHERE b.weekcode in (select max(weekcode) from attr)

SELECT TAB1.ID,TAB1.MTHCODE,TAB3.WEEKCODE,TAB3.XYZ,TAB3.ABC
FROM TAB1,TAB3
WHERE TAB1.ID=TAB3.ID(+)

if i am using these three queries its giving corretc out put.but i dont want two more tables in my database.So please help me to combine these three queries into a single query
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

The SQL may not work because the OP is not using Oracle. When posting SQL please indicate whether it is database-specific. Only Oracle prescribes outer joins in the fashion indicated.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

can you please suggest me how to make this kind of join if my data base is oracle
narasimha
Charter Member
Charter Member
Posts: 1236
Joined: Fri Oct 22, 2004 8:59 am
Location: Staten Island, NY

Post by narasimha »

Do something like this - (I have not tested it, as I dont have valid data)

Code: Select all

SELECT aa.ID, aa.mthcode, bb.weekcode, bb.xyz, bb.abc
  FROM (SELECT a.ID, a.mthcode
          FROM cbfact a
         WHERE a.mthcode IN (SELECT MAX (mthcode)
                               FROM cbfact)) aa,
       (SELECT b.ID, b.abc, b.xyz, b.weekcode
          FROM attr b
         WHERE b.weekcode IN (SELECT MAX (weekcode)
                                FROM attr)) bb
 WHERE aa.ID = bb.ID(+)
Narasimha Kade

Finding answers is simple, all you need to do is come up with the correct questions.
dr46014
Participant
Posts: 59
Joined: Fri Dec 08, 2006 11:09 am
Location: bhubaneswar

Post by dr46014 »

yesi got the result.i was also trying the same thing but i missed the subquery.
thanks a lot for your help
narasimha
Charter Member
Charter Member
Posts: 1236
Joined: Fri Oct 22, 2004 8:59 am
Location: Staten Island, NY

Post by narasimha »

Glad that helped :D
Time to mark your post as "Resolved"
Narasimha Kade

Finding answers is simple, all you need to do is come up with the correct questions.
Post Reply