Results 1 to 2 of 2
  1. #1
    Join Date
    May 2004
    Posts
    105

    Unanswered: Sql Query: trouble joining

    I need to combine these two queries where the second query will just be another column with in the first query. I want all the results from the first query displayed despite any conditions that were placed on the second query.

    Code:
    SELECT     PR.WBS1, PR.WBS2, PR.WBS3, LB.AmtBud
    FROM         PR LEFT OUTER JOIN
                          LB ON LB.WBS1 = PR.WBS1 AND LB.WBS2 = PR.WBS2 AND LB.WBS3 = PR.WBS3
    WHERE     (PR.WBS1 = '001-298') AND (PR.WBS3 <> 'ZZZ')
    ORDER BY PR.WBS2, PR.WBS3
    
    SELECT     PR.WBS1, PR.WBS2, PR.WBS3, SUM(LD.BillExt) AS Expr1
    FROM         PR LEFT OUTER JOIN
                          LD ON LD.WBS1 = PR.WBS1 AND PR.WBS2 = LD.WBS2 AND PR.WBS3 = LD.WBS3
    WHERE     (PR.WBS1 = '001-298') AND (PR.WBS3 <> 'ZZZ') AND (LD.BilledPeriod = '200408')
    GROUP BY PR.WBS1, PR.WBS2, PR.WBS3
    ORDER BY PR.WBS2, PR.WBS3
    Any suggestions?

    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT PR.WBS1
         , PR.WBS2
         , PR.WBS3
         , LB.AmtBud
         , ( select SUM(LD.BillExt) 
               from LD 
              where WBS1 = PR.WBS1 
                AND WBS2 = PR.WBS2
                AND WBS3 = PR.WBS3 
                and BilledPeriod = '200408' ) as Expr1
      FROM PR 
    LEFT OUTER 
      JOIN LB 
        ON LB.WBS1 = PR.WBS1 
       AND LB.WBS2 = PR.WBS2 
       AND LB.WBS3 = PR.WBS3
     WHERE (PR.WBS1 = '001-298') 
       AND (PR.WBS3 <> 'ZZZ')
    ORDER 
        BY PR.WBS2
         , PR.WBS3
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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