Solved

SQL statement help problem

Posted on 2010-11-16
6
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

732 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