2017-02-23 8 views
0

SQLクエリを書くのに助けが必要です。私のテーブルは:MS-SQL 2008は複数のテーブルクエリを持っています

投稿: contribution_id |金額| person_id | currency_type

Person_id |ファーストネーム|ラストネーム

関連 related_id | person_id | related_personID

通貨タイプ通貨タイプID |通貨名

他のテーブルとフィールドがありますが、これは必要なものです。ここで

は私が午前問題は、人が貢献すると最初と最後の名前は簡単ですが、証券会社が貢献したときに、私は(実在の人物姓と名を含める必要はありません。..

ですブローカレッジアカウント)。 仲介手数料はその人と同じテーブルにあります。

これまでのところ、contribution.currencyType = '12492'の場合、実際のperson_idを見つけるために関連テーブルから情報を取得する必要があります。 以下のコードを実行したときに得られることは、currencytype = 12492を除き、姓と名字がnullの場合を除いてすべてのデータです。私はこの問題を考え出し

`

declare @fund int = 165 
declare @payment_luid int = 58 
DECLARE @report_information TABLE(
           ContributionID varchar(20), 
           ContributionDate date, 
           firstname varchar(50), 
           lastname varchar(50), 
           memberID varchar(20), 
           Pledge money, 
           cash money, 
           non_cash money, 
           fund_name VARCHAR(50)) 
INSERT INTO @report_information 
SELECT c.contribution_id, 
    c.contribution_date, 
    case when c.currency_type = '12492' then t3.first_name else  t1.first_name end, 
    case when c.currency_type = '12492' then t3.last_name else t1.last_name end, 
    case when c.currency_type = '12492' THEN t3.person_id else c.person_id end as MemberID, 
    case when c.currency_type = '12492' then (select amount From ctrb_pledge where ctrb_pledge.person_id = t3.person_id and fund_id = @fund) else (select amount from ctrb_pledge where ctrb_pledge.person_id = c.person_id and fund_id [email protected]) END, 
    CASE WHEN C.currency_type_luid NOT IN (SELECT lookup_id FROM core_lookup WHERE [email protected]_luid AND lookup_qualifier2 ='1') THEN CCF.amount ELSE 0 END, 
    CASE WHEN CCF.non_cash = 1 OR C.currency_type IN (SELECT lookup_id FROM core_lookup WHERE [email protected]_luid AND lookup_qualifier2 ='1') THEN CCF.amount ELSE 0 END, 
    f.fund_name 
    FROM contribution as c 
left join core_person as t1 
on t1.person_id = c.person_id 
left join relationship as t2 
on t2.person_id = c.person_id 
left join person as t3 
on related_person_id = c.person_id 
JOIN ctrb_contribution_fund CCF ON CCF.contribution_id=C.contribution_id 
JOIN ctrb_fund F ON F.fund_id = CCF.fund_id 
where f.fund_id = @fund 
order by contribution_id 
SELECT lastname 
    ,firstname 
    ,memberID 
    ,coalesce(SUM(pledge),0) as Pledge 
    ,SUM(cash) AS cash_gifts 
    ,SUM(non_cash) AS non_cash_gifts 
    ,SUM(cash + non_cash) as TotalGiving 
    ,coalesce(SUM(pledge)-(SUM(cash)+SUM(non_cash)),0) as Balance 
    ,fund_name 
FROM @report_information 
GROUP BY memberid, lastname, firstname, fund_name 
ORDER BY lastname asc 

`

答えて

0

: はここに、これまでに私のコードです。結合ステートメントをFULL JOIN OUTERに変更すると、不足しているレコードが表示されました。

join core_person as cp 
on cp.person_id = c.person_id 
full outer join core_relationship as rel 
on rel.person_id = c.person_id 
full outer join core_person as newCp 
on rel.related_person_id = newcp.person_id 

テーブルを私が覚えているものに変更しました。 t1 = cp、t2 = rel、t3 = newcp。

関連する問題