Solved

SQL statement help problem

Posted on 2010-11-16
6
333 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how the fundamental information of how to create a table.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now