Help needed on Equi join?
I have 3 views and in that common column datatype is varchar. I have to use equi join to get the data from 3 view.
In 3 views total records are 23668,111,3033
when i wrote the join i am getting the output in lakhs. I don't whether the output is right or not.
Please guide me on this.
Waiting for valuable replies.
Thanks and Regards
Sridhar.
If you're returning more rows than you expect, it means that you have 1 to many or even many to many join.
I would re-write your query this way:
SELECT p.facid, rxs.rxno, rxs.rxbatch,patlname,patfname,InitReview,LabelPrintedOn,packed,PlacedInTote,street1,zip,BatchDescr
FROM Patients p
INNER JOIN rX.dbo.rxs rxs
ON p.facid = rxs.FacID
INNER JOIN rX.dbo.RxBatches Batch
ON p.facid= Batch.FacID
WHERE p.facid in('CRH','LSRX') and Batch.batchdescr LIKE 'STAT%'
I also suggest to add alias to every column in your query. Even if the column belongs to a particular table, adding the alias in front of it will make maintenance of this query much easier. Say, I have no idea if BatchDescr field belongs to the second table
RxBatches or rxS, so I made a guess.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles
Similar Messages
-
Help need for Outer Join Query Mysterious Query...
I have two table parent tablea and child tableb
Now
tableb has field by name table_id,keyword_name,keyword_value
tablea has field by name
table_id,table_name
I need a query which would retrieve union of this two set...(for table_name)
1)all table_name having keyword_name='abc' and value=12
2)all the table_name that does not have 'abc' as keyword in tableb.....
Please let me know how it can be done....Ok then, try this.
select
a.table_name
from
tablea a, tableb b
where
a.table_id = b.table_id
and b.keyword_name = 'abc'
and b.value = 12
union
select
c.table_name
from
tablea c
where
not exists (select 1 from tableb d
where d.table_id = c.table_id
and d.keyword_name = 'abc'
) ;To get a faster answer and avoid wrong guesses, I always find it helpful to show sample data and sample output when asking a SQL question. For example:
TableA -----------------
table_id table_name
1 AAA
2 BBB
3 CCC
4 DDD
5 EEE
TableB -----------------
table_id keyword_name value
1 abc 12
1 def 23
2 abc 13
4 def 12
5 abc 13
5 def 12
Desired output of query: -----------------
AAA
CCC
DDD -
Hi,
The follwing is my query
select imdsrequest.*,request_material.*,request.request_num from imdsrequest join request_material on
(imdsrequest.REQUEST_ID = request_material.REQUEST_ID)
join REQUEST on (request.request_id = request_material.associated_request_id)
it executes fine. But i want to re-write this query using outer join . on the above query i am using ON condition for matching the columns.
can any one help me to restructure this with outer join so that i can give all the conditions using WHERE clause .
Edited by: 936693 on Jul 3, 2012 7:12 AM
Edited by: 936693 on Jul 3, 2012 7:13 AMHi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
See the forum FAQ {message:id=9360002}
936693 wrote:
Hi Folks ,
Thanks for the reply. i don't want to use "ON" condition. So can you please show me some example based on my requirement .
i will have to use where condition instead of on and have to use outer joins where and all require, please guide me .Why don't you want to use "ON"?
Are you using Oracle 8 (or earlier)? Always say what version of Oracle you're using.
Is this part of a homework assignment, where the teacher specifically said not to use ON? Then post the exact question and instructions. It would help if you gave a little context (what was being taught in the most recent lesson) so we can give you a better answer.
The only ways to avoid "ON" are
(1) don't join any tables
(2) do a CROSS JOIN
(3) use the notation
If an outer join is appropriate for your problem, then I don't recommend any of the above. -
This is the sample piece of code in oracle which I am trying to convert it to ANSI join kindly help all the in_ and P_ and v_ are coming as a in parameter
select *
FROM fxrates_v fx1
WHERE fx1.fx_close(+) = in_fx_close AND
fx1.fxrate_currency (+) = decode (v_agree_type, 'L', upper(v_pledge_leh_unsec_curr) ,
'C', upper(v_pledge_cp_unsec_curr),
decode(sign(v_pre_mrgn_val), -1, upper(v_pledge_leh_unsec_curr),
upper(v_pledge_cp_unsec_curr)))/Michael I think your one is not equivalent to the OP's one.
Let's see an example on EMP:
SQL> select * from emp
2 where comm(+) = 300;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
SQL> select * from emp
2 where comm(+) = null;
no rows selected
SQL> select * from emp right outer join dual on comm = 300;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO D
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 X
SQL> select * from emp right outer join dual on comm = null;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO D
XIf you add the right outer join on dual and the input parameter is null you extract an all nulls row.
But the original query doesn't behave this way, the .(+) symbols have no effect on the result either if the input parameter is not null and if it is null...
This is why I simply ignored them in my re-arrangement...
So I think you can't convert the original query in ANSI sql preserving the behaviour...
Am I missing something?
Max
http://oracleitalia.wordpress.com -
Oracle table merge - JOIN help needed
Can someone help me with this results we want:
F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R C_TCN_NUM
2007-08-16 54 1 2008-02-08 4.06 131 40617700776019668
2007-08-16 54 1 2008-02-08 4.06 135 40623900776014856
2007-08-16 54 1 2007-11-02 1022.08 135 60630500002011374
2007-08-16 54 1 2008-02-08 1022.08 135 30632600004003338
Below listed are the two tables and data combinations, which need to be linked in this process.
SQL> desc test11
Name Null? Type
F_FCN_DT NOT NULL CHAR(10)
F_FCN_NUM NOT NULL NUMBER(5)
F_FCN_MED_CD NOT NULL CHAR(1)
F_APPLD_DT NOT NULL CHAR(10)
F_APPLD_AMT NOT NULL NUMBER(13,2)
F_RSN_CD NOT NULL CHAR(3)
SQL> select * from test11;
F_FCN_DT F_FCN_NUM F F_APPLD_DT F_APPLD_AMT F_R
2007-08-16 54 1 2008-02-08 4.06 131
2007-08-16 54 1 2008-02-08 4.06 135
2007-08-16 54 1 2007-11-02 1022.08 135
2007-08-16 54 1 2008-02-08 1022.08 135
SQL> desc test12
Name Null? Type
F_FCN_DT NOT NULL CHAR(10)
F_FCN_NUM NOT NULL NUMBER(9)
F_FCN_MED_CD NOT NULL CHAR(1)
F_APPLD_AMT NOT NULL NUMBER(23,6)
C_TCN_NUM NOT NULL CHAR(17)
SQL> select * from test12;
F_FCN_DT F_FCN_NUM F F_APPLD_AMT C_TCN_NUM
2007-08-16 54 1 4.06 40617700776019668
2007-08-16 54 1 4.06 40623900776014856
2007-08-16 54 1 1022.08 60630500002011374
2007-08-16 54 1 1022.08 30632600004003338Hi,
If you don't care which row from test11 gets paired with which row from test12 (as long as they're in the same group), then use the analytic ROW_NUMBER function to assign arbitrary ids to each row within each group, and add that id to the join condition, like this:
WITH test11_plus AS
SELECT f_fcn_dt, f_fcn_num, f, f_appld_dt, f_appld_amt, f_r -- or whatever you need
, ROW_NUMBER () OVER ( PARTITION BY f_fcn_dt, f_fcn_num, f, f_appld_amt -- common columns only
ORDER BY NULL
) AS r_num
FROM test11
, test12_plus AS
SELECT f_fcn_dt, f_fcn_num, f, f_appld_amt, c_tcn_num -- or whatever you need
, ROW_NUMBER () OVER ( PARTITION BY f_fcn_dt, f_fcn_num, f, f_appld_amt -- common columns only
ORDER BY NULL
) AS r_num
FROM test11
SELECT p11.* -- Or list everything except r_num
, p12.c_tcn_num
FROM test11_plus p11
JOIN test12_plus p12 ON p11.f_fcn_dt = p12.f_fcn_dt
AND p11.f_fcn_num = p12.f_fcn_num
AND p11.f = p12.f
AND p11.f_appld_amt = p12.f_appld_amt
AND p11.r_num = p12.r_num
;If the number of rows in the two tables may be different, then you may need an outer join. -
Help needed to optimize the query
Help needed to optimize the query:
The requirement is to select the record with max eff_date from HIST_TBL and that max eff_date should be > = '01-Jan-2007'.
This is having high cost and taking around 15mins to execute.
Can anyone help to fine-tune this??
SELECT c.H_SEC,
c.S_PAID,
c.H_PAID,
table_c.EFF_DATE
FROM MTCH_TBL c
LEFT OUTER JOIN
(SELECT b.SEC_ALIAS,
b.EFF_DATE,
b.INSTANCE
FROM HIST_TBL b
WHERE b.EFF_DATE =
(SELECT MAX (b2.EFF_DATE)
FROM HIST_TBL b2
WHERE b.SEC_ALIAS = b2.SEC_ALIAS
AND b.INSTANCE =
b2.INSTANCE
AND b2.EFF_DATE >= '01-Jan-2007')
OR b.EFF_DATE IS NULL) table_c
ON table_c.SEC_ALIAS=c.H_SEC
AND table_c.INSTANCE = 100;To start with, I would avoid scanning HIST_TBL twice.
Try this
select c.h_sec
, c.s_paid
, c.h_paid
, table_c.eff_date
from mtch_tbl c
left
join (
select sec_alias
, eff_date
, instance
from (
select sec_alias
, eff_date
, instance
, max(eff_date) over(partition by sec_alias, instance) max_eff_date
from hist_tbl b
where eff_date >= to_date('01-jan-2007', 'dd-mon-yyyy')
or eff_date is null
where eff_date = max_eff_date
or eff_date is null
) table_c
on table_c.sec_alias = c.h_sec
and table_c.instance = 100; -
Equi Join and Outer join using outer keyword
Hi,
First lets take the create statment for scott schema.
create table scott.emp_details(empno number, bonus_date date);
Insert Into Scott.Emp_Details Values(7369, To_Date('01-jan-2013'));
Insert Into Scott.Emp_Details Values(7499, To_Date('05-jan-2013'));
Insert Into Scott.Emp_Details Values(7521, To_Date('10-jan-2013'));
Insert Into Scott.Emp_Details Values(7566, To_Date('01-feb-2013'));
Insert Into Scott.Emp_Details Values(7654, To_Date('05-feb-2013'));
commit;lets also consider the basic scott.emp and scott.dept tables
Now I would like to equi join emp table deptno col with dept table deptno col and left outer join emp table hiredate with emp_details bonus_date and empno col in emp_details can be joined(Equi Join) with empno col of emp table if needed .The outer join has to be placed using the keyword (left/right)outer join
The select statement can have all the detials of emp table .The requirement may look weird but we have some such requirement.
Please suggestHi,
sri wrote:
Hi,
First lets take the create statment for scott schema.
create table scott.emp_details(empno number, bonus_date date);
Insert Into Scott.Emp_Details Values(7369, To_Date('01-jan-2013'));
Insert Into Scott.Emp_Details Values(7499, To_Date('05-jan-2013'));
Insert Into Scott.Emp_Details Values(7521, To_Date('10-jan-2013'));
Insert Into Scott.Emp_Details Values(7566, To_Date('01-feb-2013'));
Insert Into Scott.Emp_Details Values(7654, To_Date('05-feb-2013'));
commit;
It's best not to create your own tables in Oracle-supplied schemas, such as SCOTT. Use your own schema for your own tables.
lets also consider the basic scott.emp and scott.dept tablesI see; you're using the standard scott,emp and scott.dept tables, plus the emp_details table you posted above.
Now I would like to equi join emp table deptno col with dept table deptno col and left outer join emp table hiredate with emp_details bonus_date and empno col in emp_details can be joined(Equi Join) with empno col of emp table if needed .The outer join has to be placed using the keyword (left/right)outer join
The select statement can have all the detials of emp table .The requirement may look weird but we have some such requirement.
Please suggestThanks for posting the sample data. Don't forget to post the exact output you want from that sample data.
Do you want something like this?
` EMPNO ENAME DEPTNO DNAME BONUS_DAT
7369 SMITH 20 RESEARCH 01-JAN-13
7499 ALLEN 30 SALES 05-JAN-13
7521 WARD 30 SALES 10-JAN-13
7566 JONES 20 RESEARCH 01-FEB-13
7654 MARTIN 30 SALES 05-FEB-13
7698 BLAKE 30 SALES
7782 CLARK 10 ACCOUNTING
7788 SCOTT 20 RESEARCH
7839 KING 10 ACCOUNTING
7844 TURNER 30 SALES
7876 ADAMS 20 RESEARCH
7900 JAMES 30 SALES
7902 FORD 20 RESEARCH
7934 MILLER 10 ACCOUNTING
40 OPERATIONSIf so, here's one way to do it:
SELECT e.empno, e.ename -- or whatever columns you want
, d.deptno, d.dname -- or whatever columns you want
, ed.bonus_date
FROM scott.dept d
LEFT OUTER JOIN scott.emp e ON e.deptno = d.deptno
LEFT OUTER JOIN emp_details ed ON ed.empno = e.empno
ORDER BY e.empno
; -
May I know the difference in processing time or execution time when we use the Equi-Join and when we use Suquery in Search criteria.
Plz help, because the query (137 query) which we have written contains Subquery and fetching thousands of records, taking processing time 10 min.
We also altered the query batch with Equi-Joins and it is taking only 5 min. So can we say that Equi-Joins are much faster then Subquery.
Do Reply.
Thank in Advance
Vishal
(Database Developer)In theory, it shouldn't matter. If you are able to figure out how to unnest subquery, the optimiser query transformation engine would likely to be able to do that that as well.
-
hi, i have a problem that i have bought blackberry 8520.i want to switch my contacts from my old mobile samsung star s5230 to blackberry 8520.i have shifted my contacts from samsung to my laptop but i am unable to transfer from laptop to blackberry.i have tried to send some contacts to blackberry from laptop but blackberry doesnot show it.please let me know how can i do it.i have also downloaded blackberry desktop software but i dont know how to shift contacts from laptop to blackberry.
Duplicate...see:
http://supportforums.blackberry.com/t5/BlackBerry-Curve/help-needed-to-move-contacts/m-p/1287437#nob...
Occam's Razor nearly always applies when troubleshooting technology issues!
If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post. Please click here and read, along with the threads to which it links, for helpful information to guide you as you proceed. I always recommend that you treat your BlackBerry like any other computing device, including using a regular backup schedule...click here for an article with instructions.
Join our BBM Channels
BSCF General Channel
PIN: C0001B7B4 Display/Scan Bar Code
Knowledge Base Updates
PIN: C0005A9AA Display/Scan Bar Code -
Equi join v/s natural join
hi
i am using oracle 9.0.1.1.1...
and using hr schema table employees and departments..when i use equi join on both table it return 107 ...when perform natural join it return 32 rows,,i read in books equi and natural join is same ....why both r return diffrent answer...if they r same...The following sample data was used:
WITH emp as
SELECT 'joe' AS emp, 1 AS dept_id FROM DUAL UNION ALL
SELECT 'jill' AS emp, 1 AS dept_id FROM DUAL UNION ALL
SELECT 'jack' AS emp, 1 AS dept_id FROM DUAL UNION ALL
SELECT 'jane' AS emp, 2 AS dept_id FROM DUAL UNION ALL
SELECT 'jeb' AS emp, 2 AS dept_id FROM DUAL UNION ALL
SELECT 'jed' AS emp, 2 AS dept_id FROM DUAL UNION ALL
SELECT 'joseph' AS emp, 3 AS dept_id FROM DUAL UNION ALL
SELECT 'jackie' AS emp, 3 AS dept_id FROM DUAL
dept as
SELECT 'accounting' as dept_name, 1 as dept_id, 2 as real_dept_id FROM DUAL UNION ALL
SELECT 'IT' as dept_name, 2 as dept_id, 4 as real_dept_id FROM DUAL UNION ALL
SELECT 'hr' as dept_name, 3 as dept_id, 6 as real_dept_id FROM DUAL
)NATURAL JOIN (implicitly joins on dept_id columns because of the same column name):
SELECT emp,dept_name
FROM emp NATURAL JOIN dept
EMP DEPT_NAME
joe accounting
jill accounting
jack accounting
jane IT
jeb IT
jed IT
joseph hr
jackie hrEQUIJOIN, you must specify the columns. In this case if you specify the same join condition the NATURAL JOIN chose you will get the same results:
SELECT emp,dept_name
FROM emp JOIN dept ON dept.dept_id = emp.dept_id
EMP DEPT_NAME
joe accounting
jill accounting
jack accounting
jane IT
jeb IT
jed IT
joseph hr
jackie hrHowever, if the real join column was REAL_DEPT_ID then the NATURAL JOIN cannot satisfy that condition because the column names are different. You would have to use the EQUIJOIN
SELECT emp,dept_name
FROM emp JOIN dept ON dept.real_dept_id = emp.dept_id
EMP DEPT_NAME
jane accounting
jeb accounting
jed accountingHope this helps! -
Equi join-same output get from subquery??
Hi,
I written one equi-join query, same ouput can i get from sub query??.
select e.empno,e.ename from emp e,dept d
where e.deptno=d.deptno;
Regards,
Venkat.Really have no idea what you mean.
If you need those d. columns, just add them to your select list.
There's no subquery here at all. -
Broken LCD!!! help needed!
Hello, My 8900 now has a broken LCD and I cannot seem to find where to buy a new one! does anyone knows! help needed!
Try HorizonWireless, check the link below in my signature.
They are reputable parts dealers.
1. If any post helps you please click the below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!
3. Install free BlackBerry Protect today for backups of contacts and data.
4. Guide to Unlocking your BlackBerry & Unlock Codes
Join our BBM Channels (Beta)
BlackBerry Support Forums Channel
PIN: C0001B7B4 Display/Scan Bar Code
Knowledge Base Updates
PIN: C0005A9AA Display/Scan Bar Code -
Troubleshooting help needed: My iMac keeps crashing and restarting with a notice: "Spinlock application timed out" What can I do?
Launch the Console application in any of the following ways:
☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
☞ Open LaunchPad. Click Utilities, then Console in the page that opens.
Select the most recent panic log under System Diagnostic Reports. Post the contents — the text, please, not a screenshot. In the interest of privacy, I suggest you edit out the “Anonymous UUID,” a long string of letters, numbers, and dashes in the header and body of the report, if it’s present (it may not be.) Please don't post shutdownStall, spin, or hang reports. -
help needed for writing query
i have the following tables(with data) as mentioned below
FK*-foregin key (SUBJECTS)
FK**-foregin key (COMBINATION)
1)SUBJECTS(table name)
SUB_ID(NUMBER) SUB_CODE(VARCHAR2) SUB_NAME (VARCHAR2)
2 02 Computer Science
3 03 Physics
4 04 Chemistry
5 05 Mathematics
7 07 Commerce
8 08 Computer Applications
9 09 Biology
2)COMBINATION
COMB_ID(NUMBER) COMB_NAME(VARCHAR2) SUB_ID1(NUMBER(FK*)) SUB_ID2(NUMBER(FK*)) SUB_ID3(NUMBER(FK*)) SUBJ_ID4(NUMBER(FK*))
383 S1 9 4 2 3
384 S2 4 2 5 3
---------I actually designed the ABOVE table also like this
3) a)COMBINATION
COMB_ID(NUMBER) COMB_NAME(VARCHAR2)
383 S1
384 S2
b)COMBINATION_DET
COMBDET_ID(NUMBER) COMB_ID(FK**) SUB_ID(FK*)
1 383 9
2 383 4
3 383 2
4 383 3
5 384 4
6 384 2
7 384 5
8 384 3
Business rule: a combination consists of a maximum of 4 subjects (must contain)
and the user is less relevant to a COMB_NAME(name of combinations) but user need
the subjects contained in combinations
i need the following output
COMB_ID COMB_NAME SUBJECT1 SUBJECT2 SUBJECT3 SUBJECT4
383 S1 Biology Chemistry Computer Science Physics
384 S2 Chemistry Computer Science Mathematics Physics
or even this is enough(what i actually needed)
COMB_ID subjects
383 Biology,Chemistry,Computer Science,Physics
384 Chemistry,Computer Science,Mathematics,Physics
you can use any of the COMBINATION table(either (2) or (3))
and i want to know
1)which design is good in this case
(i think SUB_ID1,SUB_ID2,SUB_ID3,SUB_ID4 is not a
good method to link with same table but if 4 subjects only(and must) comes
detail table is not neccessary )
now i am achieving the result by program-coding in C# after getting the rows from oracle
i am using oracle 9i (also ODP.NET)
i want to know how can i get the result in the stored procedure itsef.
2)how it could be designed in any other way.
any help/suggestion is welcome
thanks for your time --PradeeshWell I forgot the table-alias, here now with:
SELECT C.COMB_ID
, C.COMB_NAME
, (SELECT SUB_NAME
FROM SUBJECTS
WHERE SUB_ID = C.SUB_ID1) AS SUBJECT_NAME1
, (SELECT SUB_NAME
FROM SUBJECTS
WHERE SUB_ID = C.SUB_ID2) AS SUBJECT_NAME2
, (SELECT SUB_NAME
FROM SUBJECTS
WHERE SUB_ID = C.SUB_ID3) AS SUBJECT_NAME3
, (SELECT SUB_NAME
FROM SUBJECTS
WHERE SUB_ID = C.SUB_ID4) AS SUBJECT_NAME4
FROM COMBINATION C;
As you need exactly 4 subjects, the columns-solution is just fine I would say. -
Hi all help needed I have a canon 40D. I am thinking of buying a canon 6D.
But not sure that my lenses will work.
I have a 170mm/ 500mm APO Sigma.
A 10/20 ex Sigma HSM IF.
And a 180 APO Sigma Macro or do I have to scrap them and buy others.
ALL Help will be greatly received. Yours BRODIEIn short, I love it. I was going to buy the 5DMark III. After playing with it for a while at my local Fry's store where they put 5DMII, 5DMIII and 6D next to each other, using the same 24-105L lens, I decided to get the 6D and pocket the different for lens later.
I'm upgrading from the 30D. So I think you'll love it. It's a great camera. I have used 5DMII extensively before (borrowing from a close friend).
Funny thing is at first I don't really care about the GPS and Wifi much. I thought they're just marketing-gimmick. But once you have it, it is actually really fun and helpful. For example, I can place the 6D on a long "monopod", then use the app on the phone to control the camera to get some unique perspective on some scenes. It's fun and great. GPS is also nice for travel guy like me.
Weekend Travelers Blog | Eastern Sierra Fall Color Guide
Maybe you are looking for
-
The laptop is less than a year old, and I hadn't used Firefox since I got it. I'm returning to Firefox now, but it's version 3.5.8 and I want 3.6. I check for software updates, but no FF update is offered. I try to download 3.6 from the Mozilla site,
-
DAC Physical Data Source password change - invalid username/password
We are using DAC/Informatica for loading Peoplesoft data into the warehouse. The password for the peoplesoft source was changed recently. I changed the password in DAC under setup, Type source for the Peoplesoft DB. When I select 'Test Connection' I
-
Hi...I have a laptop and I have had problems with skype home in the past, but not it will not work at all. It keep spinning. I am using windows 7 professional. Last nite I used piriform defraggler instead of the one built into my computer and now sk
-
Why doesnt my keylistener work!?!
class ers { public static void main (String [] args){ int width = 500; int height = 500; int keyR=0; Slate slate= new Slate (width,height); Graphics g = slate.getSlateGraphics(); g.setColor (Color.white); draw (g,0,0,width,height); slate.repaint(); K
-
Is there anyway to 1. Fix the size on JScrollPane area on the screen. 2. Enable only the vertical scrollbar. So that user can scrolling vertically for the content within the specified Scroll Pane area.