Need SQL Help...Can't Figure out a 3 table JOIN

Need SQL Help...Can't Figure out a 3 table JOIN

Post by Steve Trees » Sun, 09 Feb 1997 04:00:00



I sure could use some help from the "SQL experts"!!   I've been trying to
run
the following query and get nothing but correllation errors.   I'm trying
to
display the Site Names which are linked to each Program Name using a
many-to-
many relationship table (SITE_TO_PGM).

SELECT   PROGRAMS.PROGRAM_NAME,    SITES.SITE_NAME

FROM     PROGRAMS,   SITES,     SITE_TO_PGM

WHERE    SITES.SITE_ID = SITES_TO_PGM.SITE.ID
         AND SITES_TO_PGM.PROGRAM_ID = PROGRAMS.PROGRAM_ID

Expected Output:

PROGRAM NAME 1  
   SITE NAME 1
   SITE NAME 2
   SITE NAME 3

PROGRAM NAME 2
   SITE NAME 4
   SITE NAME 5

Can someone please help me out?

---
Do I need a KEY JOIN?  I've tried this:

SELECT   PROGRAM_NAME,   SITE_NAME

FROM     PROGRAMS
         KEY JOIN SITES
         KEY JOIN SITE_TO_PGM

but get "NO WAY TO JOIN SITES to PROGRAMS"

---

Here's my 3 tables.  * = Primary Key

SITES Table            
===========                            

SITE_ID*        SITE_NAME
-------         ---------                                      
1               Site Name 1
2               Site Name 2
3               Site Name 3
4               Site Name 4
5               Site Name 5

SITE_TO_PGM Table
=================
SITE_ID*        PROGRAM_ID*
-------         ----------
1               1
2               1
3               1
4               2
5               2

PROGRAMS Table
==============
PROGRAM_ID*     PROGRAM_NAME
----------              ------------
1                       Program Name 1
2                       Program Name 2
3                       Program Name 3
4                       Program Name 4

Thanks...

Steve

 
 
 

Need SQL Help...Can't Figure out a 3 table JOIN

Post by Jasper St » Mon, 10 Feb 1997 04:00:00



> I sure could use some help from the "SQL experts"!!   I've been trying to
> run
> the following query and get nothing but correllation errors.   I'm trying
> to
> display the Site Names which are linked to each Program Name using a
> many-to-
> many relationship table (SITE_TO_PGM).

> SELECT   PROGRAMS.PROGRAM_NAME,    SITES.SITE_NAME

> FROM     PROGRAMS,   SITES,     SITE_TO_PGM

> WHERE    SITES.SITE_ID = SITES_TO_PGM.SITE.ID
>          AND SITES_TO_PGM.PROGRAM_ID = PROGRAMS.PROGRAM_ID

The query seems OK to me, except for the second period in
"SITES_TO_PGM.SITE.ID" (I suppose that's a typo?) and the fact that
you will receive output in a grid, not as stated below. What error(s)
do you get?

Jasper

 
 
 

1. Can't Figure Out - Help needed quickly

I cannot get this to work.  I continue to get a "Order Date not part of
aggregate function" error on run in access query.  When I add Order_Date to
group by it then gives me the error message with "Ship Date"

Any Ideas

SELECT
    Orders.Order_ID,
    Orders.Order_Date as [Order Date],
    Orders.Ship_Date as [Ship Date],
    Shipping_Methods.Shipping_Method as Via,
    Orders.Freight_Charge as Shipping,
    Orders.Sales_Tax_Rate as Tax,
    (Order_Detail.unit_Price *  Order_detail.Quantity) +
(Order_Detail.unit_Price  *  Order_detail.Quantity
Orders.Sales_Tax_Rate) +     Orders.Freight_Charge  AS [Total Order]
FROM
    Orders,
    Shipping_Methods,
    Order_detail
WHERE
    Orders.Ship_Method_ID =     hipping_Methods.Shipping_Method_ID
AND
    orders.Order_ID = Order_Detail.OrderID
AND
    oRDERS.Customer_ID = 1

group by Orders.Order_ID

2. VFP 5.0

3. sql guru's - need help with an inner join

4. SQL syntax help

5. Ok..I'm stumped and need help...SQL Query w/ Join

6. ITW Paslode Chooses FourGen and Informix

7. Need Help with SQL... 3 Table JOIN

8. Recordset can't be updated

9. need help please:joined tables,add new entries based on one table columns

10. Help needed to figure out SQL Statement....

11. I can't edit table where I join two table (SQL 6.5)