2011-05-09 88 views
0

in Image table - 1 is table with Primery key ID autonumber and DeclarationContentId as FK from another table. There are two column GrantedAmount and DeclaredAmount Identified by the another column AmountType. I want a left outer join query which will return me a for DeclartionContentId with GrantedAmount and DeclaredAmount togather in a single row extepecte output table shown below in the image左外聯接查詢

- 圖1是表與從另一個表Primery密鑰ID自動編號和作爲DeclarationContentId FK。有兩列GrantedAmount和DeclaredAmount由另一列AmountType標識。我希望有一個左外連接的查詢將返回我的DeclartionContentId與GrantedAmount和DeclaredAmount togather在單行

+0

請澄清你的問題和/或提供預期結果的一個例子。 – 2011-05-09 05:04:12

+0

你叫什麼兩張表?第一個表中有哪些列;哪些列在第二個表中?爲什麼你想要一個LOJ查詢 - 而不是一個普通的內部連接? – 2011-05-09 05:05:16

+0

@Johathan假設在另一個表中只有一列,並且只有一個列的DeclarationContentId爲PK。如果內部加入它的可能性有預期的結果它與我確定。 – pvc 2011-05-09 05:13:00

回答

0

使這裏的一些假設

  • 這兩個表是declarationcontent,你想declarationcontentamount
  • 所授予的量時,類型1
  • 你想申報量時,類型爲2
  • 你想零,而不是空
  • 左加入,因爲每一種類型可能不表示每個declarationcontentid

試試這個

SELECT dc.declarationcontentid, 
     Isnull(ga.grantedamount, 0) grantedamount, 
     Isnull(dc.declaredamount, 0) declaredamount 
FROM declarationcontent dc 
     LEFT JOIN declarationcontentamount ga 
     ON dc.declarationcontentid = ga.declarationcontentid 
      AND amounttype = 1 
     LEFT JOIN declarationcontentamount dc 
     ON dc.declarationcontentid = ga.declarationcontentid 
      AND amounttype = 2