?
Solved

SQL statement help problem

Posted on 2010-11-16
6
Medium Priority
?
387 Views
Last Modified: 2012-08-14
Hi all.

I am struggling with a sql requirement. I have a main table 1 record. Then 4 many ot many tables.

mt = main table
t1 = many to many table
t2 = many to many table
t3 = many to many table
t4 = many to many table

What i want is to get all the data from each into there columnss but now have any duplicate data from any table.

eg if t1 has 3 rows and t2 has 2 rows and t3 has 1 and t4 as 1 then when i do the select i just want the select like:

    01 | S56Z  |  123   |  989              |      4840713
    01 | 070A  |  456    |                      |      
    01 | V60A  |             |                      |      

not 1 row per every posssible different possibility which is the default in sql.

Hope this makes sense to someone. I cannot think of a way to do it without using loops and table variables.


0
Comment
Question by:philipeharris
6 Comments
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34143725
Are there any columns in these tables wich join then to eachother, or are you just trying to show records from 5 tables at the same time?
0
 
LVL 3

Expert Comment

by:boraganesh
ID: 34143873
these all 5 tables have same structure?
0
 
LVL 2

Expert Comment

by:AvalonSA
ID: 34144691
If all the tables have the same structure you can select
t1
union
t2
union
t3
.........

It'll automaticly distinct the duplicates
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 10

Expert Comment

by:wls3
ID: 34147004
Do you have to worry about intermediate tables or can you access the tables directly?  If you can directly access the tables, you can do right joins explicitly on the tables you are joining, assuming the main table is the left join.  For example,

select *
from main as m
right join t1 as t1
on m.key = t1.key
right join t2 as t2
on m.key t2.key
.
.
.

0
 

Accepted Solution

by:
philipeharris earned 0 total points
ID: 34148138
Hi all thanks for relplies.

5 tables structures are as follow

Main table groups
groupid, lots of other fields we dont care about for this but is the main group table.

Then the 4 other tables are 1 to many tables related to the accountid
structure examples are:

table 1
grouid, code1id, sequenceid
1, beer, 1
2 lager, 2
3, vodka, 3

table 2
groupid cdeo2id,sequenceid
1, lettice, 1
1, cucumber, 2
2, potato, 1
3, lettice, 1
3, cucumber, 2
3, potato, 3

table 3
groupid code3id,sequenceid
1, golf, 1
1, tennis, 2
1, cricket, 3
2, golf, 1
3, golf 1

table4
groupid code4id, sequenceid
1, bmw, 1
2, fiat, 1
3, skoda, 1


The sequence id in each table is related

A normal join scenario in sql will return all possible combinations but i dont want that as it would be over 12 rows for the above example for just group id 1, what i want is: if the maximium number of record codes over all 4 tables for groupid 1 is 3 so i only want 3 rowsfor tha group. All other column data needs to fit in the 4 rows. All group 1 sequesnce 1 together, then 2 then 3, then 4 so on, if the other tables 1-4 dont have records for groupid 1 and sequesnce x then a null should be displayed.

Hope this makes sense

0
 

Author Closing Comment

by:philipeharris
ID: 34186314
managed to do it. thanks all
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question