Solved

SQL statement help problem

Posted on 2010-11-16
6
357 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

822 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