0

I have two oracle select queries like

SELECT loc.location AS LOCATION , req.requisition AS REQ 
FROM location_view loc, requisition_view req, association ass 
WHERE loc.name = 'ABC' AND req.name = 'TRANSFER' 
AND ass.entity_id_2 = req.entity_id AND ass.entity_id_1 = loc.entity_id

And the result looks like:

enter image description here

Other query is like:

 SELECT req.requisition AS req, exp.experiment AS expt 
 FROM experiment_view exp, requisition_view req, association_view ass 
 WHERE expt.name = 'RETRIEVAL'AND req.name = 'TRANSFER' 
 AND ass.entity_id_2 = req.entity_id AND ass.entity_id_1 = expt.entity_id 

Result:

enter image description here

I am trying to combine these two SELECT queries so I get to see these results:

enter image description here

Should I be using Sub-Queries to see the combined result or is there any other way of optimizing?

3
  • This makes no sense. Besides obvious typos (so these are NOT the queries that produce the results you show, since they are syntactically incorrect), how does it make sense for ass.entity_id_1 to be used to match both the location_view view and the experiment_view view?
    – user5683823
    Jul 26, 2017 at 21:28
  • What syntax errors did you find? Looked valid, if a little ancient, to me!
    – Caius Jard
    Jul 26, 2017 at 21:43
  • @mathguy : Well it makes sense given that it's not the same row. There is one row in locations, 1 in requisitions, 1 in experiments and 2 in associations. The first row in associations links the loc to the req and the second row links the rep to the exp. I personally hate these multipurpose generic association tables but people keep doing them :)
    – LauDec
    Jul 26, 2017 at 23:24

3 Answers 3

1

I'm not sure the provided solutions are correct. All of them are using 1 join to the association table. You need 2. Because Association looks to be a generic mapping table so the row that joins locations to requisitions is not the same as the one which joins requisitions to experiments. Maybe i'm wrong but i'd go for :

SELECT
  loc.location as LOCATION , 
  req.requisition as REQ, 
  exp.experiment as EXPT
FROM  location_view    loc
JOIN  association      asslr ON asslr.entity_id_1 = loc.entity_id
JOIN  requisition_view req   ON asslr.entity_id_2 = req.entity_id and req.name = 'TRANSFER'
JOIN  association_view assre ON assre.entity_id_2 = req.entity_id
JOIN  experiment_view  exp   ON assre.entity_id_1 = exp.entity_id AND exp.name = 'RETRIEVAL'
WHERE loc.name = 'ABC' 
1
  • Good catch, very subtle that difference you spotted. Well doen
    – Caius Jard
    Jul 26, 2017 at 22:17
0
SELECT loc.location AS location, 
       req.requisition AS req,
       exp.experiment AS expt
  FROM location_view loc
 INNER JOIN association ass
    ON loc.entity_id = ass.entity_id_1
 INNER JOIN requisition_view req
    ON req.entity_id = ass.entity_id_2
 INNER JOIN experiment_view exp
    ON expt.entity_id = ass.entity_id_1
 WHERE loc.name = 'ABC'
   AND req.name = 'TRANSFER' 
   AND expt.name = 'RETRIEVAL'
0

The two queries are nearly identical. The results can be joined together on a common element so yes, they can just be written as one query:

select loc.location as LOCATION , req.requisition as REQ, exp.experiment as expt
from location_view loc, requisition_view req, association ass, experiment_view exp
where  loc.name = 'ABC' and req.name = 'TRANSFER' and ass.entity_id_2 = req.entity_id and ass.entity_id_1 = loc.entity_id and ass.entity_id_1 = expt.entity_id and expt.name = 'RETRIEVAL'

This is a bit of an ancient nonstandard way to write queries though; look into how the INNER JOIN keywords work; here's how I'd have laid this query out:

select
  loc.location as LOCATION , 
  req.requisition as REQ, 
  exp.experiment as expt
from 
  association ass
  INNER JOIN
  location_view loc
  ON
    ass.entity_id_1 = loc.entity_id

  INNER JOIN 
  requisition_view req 
  on 
    ass.entity_id_2 = req.entity_id

  INNER JOIN
  experiment_view expt
  ON
    ass.entity_id_1 = expt.entity_id

WHERE        
  loc.name = 'ABC' and
  req.name = 'TRANSFER' and
  expt.name = 'RETRIEVAL'
2
  • I tried the first statement with out using joins but it is not returning any records
    – trx
    Jul 26, 2017 at 22:02
  • @trx Try laudec's answer; he notices that one of your query uses association table, other uses association_view table. I didn't notice this and it could be a critical difference.. another good reason why you should lay your sql out nicely; makes them easier to read!
    – Caius Jard
    Jul 26, 2017 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.