Solved

SQL statement help problem

Posted on 2010-11-16
6
383 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
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.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

690 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