Hi,
Can you explain your question better ?
Thanks.
Main Topics
Browse All TopicsHello Everyone!
How to resolve the cartasion in SQL Query.
Eg. If a master in joined to 2 detailed record and further 2 more detail record.
For all suggesstion/help, I thank in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hello John & Albert,
Thanks for your comments, yes I think i was not descriptive in my question sorry for that, actually the follwing sql
Select e.eqpt_id,
e.eqpt_name,
t.tech_name,
s.part_name
from equipment_master e,
tech_master t,
spare_parts s
where e.eqpt_id = t.eqpt_id and
e.eqpt_id = s.eqpt_id
where I want to avoid cartesian.
The scenario is like this, One equipment is maintained by two technician and they have used two spare parts.
but the query result show that the spare used are four instead two only (repeating the same two spares). How to avoid this ?
Thanks once again for your time.
Hi
Sorry, but I disagree with the use of the DISTINCT clause "osbjmq". It might give you what you want IN THIS CASE, but it masks a more fundamental problem with the data or in the design of the tables.
Taking a quick look at the comments without having access to the structures of the 3 tables involved, I would hazard a guess that the SPARE_PARTS table might have a composite primary key which contains BOTH the key of EQUIP_MASTER and TECH_MASTER ?
Please post the table structures (significant columns only if you like) and show the primary keys on each table.
I think this might help shed more light on the problem.
Cheers
Hello!!
Pls find the table structures
Table 1
Job_order_Number PK
Eqpt_id
Eqpt_name
Maintenance_date
Record
M10001,EQP-1002,GENERATOR,
Table 2
Job_order_number FK
Tech_id
Tech_name
Man_hours
Labor_rate
EX. DATA
M10001,1020,VVVVVVV,20,45
M10001,1022,TTTTTTT,20,45
Table 3 [detail table]
Job_order_number FK
Spare_id
Spare_name
Qty
unit_cost
EX. DATA
M10001,DE12304,UUUUUUUUUUU
M10001,DF12349,JJJJJJJJJJJ
The expected result should be as follows;
M10001,VVVVVVV,900,UUUUUUU
M10001,TTTTTTT,900,JJJJJJJ
But the result we get is
M10001,VVVVVVV,1800,UUUUUU
M10001,TTTTTTT,1800,JJJJJJ
Hope this is would be much clearer in my question.
Thanks once again for all valuable suggestion.
Regards
Hi
The reason you are getting twice as many records as you want is because there are actually 2 separate parts to the question you are trying to answer as 1 question.
1 - Who has worked on this equipment and how many hours have they spent and what is their total cost?
2 - How many spare parts have been used for this equipment and what is the total cost?
Results of queries:
1 - M10001,VVVVVVV,900
M10001,TTTTTTT,900
2 - M10001,UUUUUUUUUUUUUU,690.
M10001,JJJJJJJJJJJJJJ,1048
If you combine them as you seem to want to, you get the output as follows:
M10001,VVVVVVV,900,UUUUUUU
M10001,TTTTTTT,900,UUUUUUU
M10001,VVVVVVV,900,JJJJJJJ
M10001,TTTTTTT,900,JJJJJJJ
The output you want to achieve is as follows:
M10001,VVVVVVV,900,UUUUUUU
M10001,TTTTTTT,900,JJJJJJJ
If I was asked to interpret this output I would say that Technician VVVVVVV has worked on equipment M10001 and has used (or fitted) spare part UUUUUUUUUUUUUU to it, and that Technician TTTTTTT has worked on equipment M10001 and has used (or fitted) spare part JJJJJJJJJJJJJJ to it.
In other words there is an implied relationship between the Technician and the spare part in the output. There may be a way to get the output you want in the way you want it but I would doubt the usefulness of the data output in this way.
If I was asked to provide this kind of information using a single query, the only way in which I can think of trying to provide a meaningful answer would be to provide the following information:
Equip Id Equip Name Total Labour Cost Total Parts Cost
-------- ---------- ----------------- ----------------
M10001 GENERATOR 1800 1738.00
If you need to find out which parts or which technician had worked on the equipment, this could be provided as 2 separate queries.
I think that you should re-analyse your requirement to display the data in this way and try to come up with a more meaningful way of showing the relationships between the 3 tables.
HTH
Mark
Hi
The reason you are getting twice as many records as you want is because there are actually 2 separate parts to the question you are trying to answer as 1 question.
1 - Who has worked on this equipment and how many hours have they spent and what is their total cost?
2 - How many spare parts have been used for this equipment and what is the total cost?
Results of queries:
1 - M10001,VVVVVVV,900
M10001,TTTTTTT,900
2 - M10001,UUUUUUUUUUUUUU,690.
M10001,JJJJJJJJJJJJJJ,1048
If you combine them as you seem to want to, you get the output as follows:
M10001,VVVVVVV,900,UUUUUUU
M10001,TTTTTTT,900,UUUUUUU
M10001,VVVVVVV,900,JJJJJJJ
M10001,TTTTTTT,900,JJJJJJJ
The output you want to achieve is as follows:
M10001,VVVVVVV,900,UUUUUUU
M10001,TTTTTTT,900,JJJJJJJ
If I was asked to interpret this output I would say that Technician VVVVVVV has worked on equipment M10001 and has used (or fitted) spare part UUUUUUUUUUUUUU to it, and that Technician TTTTTTT has worked on equipment M10001 and has used (or fitted) spare part JJJJJJJJJJJJJJ to it.
In other words there is an implied relationship between the Technician and the spare part in the output. There may be a way to get the output you want in the way you want it but I would doubt the usefulness of the data output in this way.
If I was asked to provide this kind of information using a single query, the only way in which I can think of trying to provide a meaningful answer would be to provide the following information:
Equip Id Equip Name Total Labour Cost Total Parts Cost
-------- ---------- ----------------- ----------------
M10001 GENERATOR 1800 1738.00
If you need to find out which parts or which technician had worked on the equipment, this could be provided as 2 separate queries.
I think that you should re-analyse your requirement to display the data in this way and try to come up with a more meaningful way of showing the relationships between the 3 tables.
HTH
Mark
I understand that you like to create something like a BOM (bill of material), right.
You have no reference store which spare part has been used by which technician, so it doesn't make any sense to look for a query that combines a technician and a spare part in a single row. What should happen for example if more spare parts than technicians involed (or vice versa) have used?
For a BOM you should query independantly spares and technicians and merge the results using a UNION ALL. You can also define a view that does this job.
For a two-column report you can go like this (I warn you it is really ugly)
with bom as (select rownum mr from
tech
where ic='generator'
union
select rownum mr from
spare where ic='generator' ),
spares as
( select rownum sr, name, u*p up from spare
where ic='generator')
, techs as
( select rownum sr, name, h*p hp from tech
where ic='generator')
select 'generator', spares.name, spares.up, techs.name, techs.hp
from bom left join spares on (mr=sr) left join techs on (mr=sr)
/
Business Accounts
Answer for Membership
by: osbjmgPosted on 2002-11-10 at 22:23:42ID: 7432438
I am not sure about what you are asking, but a cartesian product is made whenever you add relations(tables) into the FROM clause;
SELECT A.stagename, F.filmtitle
FROM actors a, films f, casts c
WHERE a.actorid=c.actorid AND
c.filmid=f.filmid AND
F.filmtitle="Top Gun";
this query joins three tables, ACTORS, FILMS, and CASTS in the FROM clause. CASTS contains imformation about a specific set of actors and the movie they acted in, this query should return all actors names that were in the Top Gun cast (assuming you have this huge movie database).
Think of a cartesian product as multiplying the rows(tuples) by the size of the other table's rows. a table that has 10 rows and is joined with a table also containing 10 rows, the resulting cartesian product is 100 rows.
In short, the more tables in the FROM clause, the slower the query will be; almost exponentially. Every added table is multiplying the previous resulting table by its number of rows.
Table1=20 rows
Table2=20 rows
Table3=20 rows
Table4=20 rows
joining Table1, Table2, Table3, Table4 = 160,000rows
now your WHERE conditions must go through and do 160,000 comparisons for each WHERE condition (i.e.-a.actorid=c.actorid)
The WHERE conditions select only the resulting rows where one of the WHERE conditions is true.
Hope that helps, if not please clarify so I might help.
John Gill