Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364

    Unanswered: Correlated Subquery

    Wonder if anyone can help

    If I wanted 2 Values from a Correlated Subquery (same table join etc) would i have to have 2 subqueries or is there a better way to do it ?

    ie.
    Code:
    SELECT t1.MyMainCode, 
    (SELECT SUM(t2.Qty) FROM t2 WHERE t2.MyMainCode = t1.MyMainCode) tQty, 
    (SELECT SUM(t2.Qty2) FROM t2 WHERE t2.MyMainCode = t1.MyMainCode) tQty2 
    FROM t1
    Any Help appreciated

    GW
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select t1.MyMainCode
         , sum(t2.Qty)
         , sum(t2.Qty2)
      from t1
    left outer
      join t2 
        on t1.MyMainCode = t2.MyMainCode
    group
        by t1.MyMainCode
    rudy
    http://r937.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •