2

I need to select data from four tables based on only one.

In my 'calculated' table, I have all the records I need.

But I need to retrieve some other info for each record, from 'programs', 'term' and 'imported' tables.

'calculated' has ID from 'programs'.

But, to achieve a record from 'imported', I need to join the 'item' table, because 'item' has ID from 'programs' and from 'imported'.

'term' has ID from 'imported'.

So, I tried this:

select  c.date,
    p.name,
    c.name1,
    c.name2,
    t.date,
    i.version,
    c.price1,
    c.price2,
    c.price3
from calculated c, programs p, term t, imported i, item it
where c.programs_id = p.programs_id
    and c.programs_id = it.programs_id
    and it.imported_id = i.imported_id
    and i.term_id = t.term_id;

But when I use count(*) on 'calculated', I get 30k of records, and from my select statement I get more than 130 millions of records.

What am I doing wrong?

What should I do for this to work?

2 Answers 2

0

If all duplicates rows are equivalent, u can try smth like this

select  c.date,
    p.name,
    c.name1,
    c.name2,
    t.date,
    i.version,
    c.price1,
    c.price2,
    c.price3
from calculated c, programs p, term t, imported i
where c.programs_id = p.programs_id and
(select imported_id from item it where c.programs_id = it.programs_id and rownum = 1) = i.imported_id
and i.term_id = t.term_id;

where "rownum = 1" is restriction on the selection of one line for oracle.

1
  • Thanks for the help Koroed, but today I was told that's no longer needed use this sql statement. I was able then to retrieve the data successfully. Thank you all for the help. I appreciate it.
    – LuizFranco
    Mar 1, 2013 at 21:38
0

you forgot to join term table.

Probably you need to add

and t.term_id = i.term_id
3
  • Hi Cha, I had already corrected my sql statement. The term table was already joined to, but was with wrong alias.
    – LuizFranco
    Feb 28, 2013 at 3:16
  • 1
    OK, so in this case you probably have one-to-many relationship in some of your tables. E.g. first that comes to mind is that there are multiple items (table item) are imported in one batch (table imported). Try to exclude tables one by one to see which one causes the duplication
    – cha
    Feb 28, 2013 at 3:38
  • Thank you Cha. I found that the join between calculatedand item causes the duplication. So, how do I fix this?
    – LuizFranco
    Feb 28, 2013 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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