Левый запрос на внешнее соединение - PullRequest
0 голосов
/ 09 мая 2011

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 - это таблица с автонумерацией идентификатора первичного ключа и объявлениемConContentId в виде FK из другой таблицы.Есть два столбца GrantedAmount и DeclaredAmount, идентифицированные другим столбцом AmountType.Я хочу запрос левого внешнего соединения, который будет возвращать мне для DeclartionContentId с GrantedAmount и DeclaredAmount для объединения в одну строку

1 Ответ

0 голосов
/ 09 мая 2011

Делая некоторые предположения здесь

  • Две таблицы: declarationcontent и declarationcontentamount
  • вы хотите, чтобы сумма была предоставлена, когда тип 1
  • вы хотите объявленную сумму, когда тип 2
  • Вы хотите нули вместо нуля
  • Левые соединения, потому что каждый тип не может быть представлен для каждого объявленияcontentid

Попробуйте это

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 
...