Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query Parent Table with More Than Two Child Tables

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-05-07 : 11:03:56
George writes "How do you write the most efficient SQL statement to query one parent table with more than two tables? For example:

User - Main table
User_Line_Item - Parent Table
Line_Item_Monitor - Child Table
Line_Item_Computer- Child Table
Line_Item_Printer - Child Table

If you want to get the following information:

- User - First_Name
- User_Line_Item - Purchase_Date
- Line_Item_Monitor - Resolution
- Line_Item_Computer - Processor
- Line_Item_Printer - Cartridge

How can you write the most effective SQL statement to display the above information in one line (one record - First_Name, Purchase_Date, Resolution, Processor, Cartridge)? Do you use UNION or create VIEW and How? Please give an SQL statement example."

DavidD
Yak Posting Veteran

73 Posts

Posted - 2002-05-07 : 20:59:01
select a.first_name, b.Purchase_Date, c.Resolution, d.Processor, e.Cartridge
from user a join User_Line_Item b on a.linkfield = b.linkfield join Line_Item_Monitor c on b.linkfield = c.linkfield join Line_Item_Computer d on b.linkfield = d.linkfield join Line_Item_Printer e on b.linkfield = e.linkfield

This is a basic SQL Query, you can use it to create a view if you wish, but it depends what purpose the query is for if that is necessary (ie who is accessing the resultset? How? Security considerations etc).
A union query would not be used for this scenario.

Regards
DD

Go to Top of Page
   

- Advertisement -