0

以前、MYSQL を介して複数のテーブルを結合する方法について質問しましたが、新しい問題が発生しました。

これらのいくつかのテーブルがあるとします。

償還表

-Name---Check Number-----Date------Reimbursement--
-John-----1--------------7/23/12-------$100-------
-Joe------2--------------7/23/12-------$102-------
-Jane-----3--------------7/23/12-------$103-------
-Jill-----4--------------7/23/12-------$104-------

元の主張

-Name---Check Number-----Date---------Amount--
-John-----1--------------7/23/12-------$100---
-Joe------2--------------7/23/12-------$102---
-Jane-----3--------------7/23/12-------$103---
-Jill-----4--------------7/23/12-------$100---

調整済み請求

-Name---Check Number-----Date---------Amount--
-Jill-----4--------------7/23/12-------$4-----

結晶レポートによる最終結果は、

-Name---Check #---Claim Type----Date Paid----Amount----Reimbursement--
-John-----1-------Original--------7/23/12-----$100-------$100---------
-Joe------2-------Original--------7/23/12-----$102-------$102---------
-Jane-----3-------Original--------7/23/12-----$103-------$103---------
-Jill-----4-------Original-------7/23/12------$100-------$104---------
-Jill-----4-------Adjusted-------7/23/12------$4----------------------

いいえ

-Name---Check #---Claim Type----Date Paid----Amount----Reimbursement--
-John-----1-------Original--------7/23/12-----$100-------$100---------
-Joe------2-------Original--------7/23/12-----$102-------$102---------
-Jane-----3-------Original--------7/23/12-----$103-------$103---------
-Jill-----4-------Original-------7/23/12------$100-------$104---------
-Jill-----4-------Adjusted-------7/23/12------$4---------$104---------

私の現在のスクリプトは次のとおりです。

Select  mt.`Check Number` as `Check Number`,
        mt.`Payment Date` as `Payment Date`,
        mt.`Payment Type` as `Payment Type`,
        c1.`Facility Name` as `Facility Name`,
        'Adjustment June 2012' as `Claim Type`,
        c1.`Amount` as Amount,
        mt.Reimbursement as Reimbursement 
  From  Reimbursement mt 
               Inner Join ClaimAdjust_6_2012 c1 
                  on c1.`Check Number`  = mt.`Check Number` and 
                     mt.`Payment Date` = c1.`Payment Date` 
  Where mt.`Payment Date`>='2012-7-1' And 
        mt.`Payment Date`<'2012-8-1' 

Union 

Select  mt.`Check Number` as `Check Number`,
        mt.`Payment Date` as `Payment Date`,
        mt.`Payment Type` as `Payment Type`,
        c1.`Facility Name` as `Facility Name`,
        'Original June 2012' as `Claim Type`,
        c1.`Amount` as Amount,
        mt.Reimbursement as Reimbursement 
 From   Reimbursement mt 
              Inner Join Claim_6_2012 c1 
                    on c1.`Check Number`  = mt.`Check Number` and 
                        mt.`Payment Date` = c1.`Payment Date` 
Where   mt.`Payment Date`>='2012-7-1' And 
        mt.`Payment Date`<'2012-8-1'  
4

2 に答える 2

1

このクエリは機能しますか? Reimbursement Tableandだけを JOIN しOriginal Claimて UNION してみませんかAdjusted Claim

SELECT      a.Name, a.CheckNumber, a.`Date`,
            b.Amount, a.Reimbursement
FROM        Reimbursement a INNER JOIN
                OrginalClaim b ON
                    a.Name = b.Name AND
                    a.CheckNmber = b.CheckNumber AND
                    a.`Date` = b.`Date`
UNION
SELECT      Name, CheckNumber, `Date`, Amount, NULL as Reimbursement
FROM        AdjustedClaim
于 2012-07-23T22:24:04.780 に答える
0

答えはとてもシンプルだと思います。最初のサブクエリの払い戻しは無視してください。

Select mt.`Check Number` as `Check Number`,mt.`Payment Date` as `Payment Date`,mt.`Payment Type` as `Payment Type`,c1.`Facility Name` as `Facility Name`,'Adjustment June 2012' as `Claim Type`,c1.`Amount` as Amount, NULL as Reimbursement
From Reimbursement mt  Inner Join
     ClaimAdjust_6_2012 c1
    on c1.`Check Number` = mt.`Check Number` and mt.`Payment Date` = c1.`Payment Date`
Where mt.`Payment Date`>='2012-7-1' And mt.`Payment Date`<'2012-8-1' 
Union all
Select mt.`Check Number` as `Check Number`,mt.`Payment Date` as `Payment Date`,mt.`Payment Type` as `Payment Type`,c1.`Facility Name` as `Facility Name`,'Original June 2012' as `Claim Type`,c1.`Amount` as Amount,mt.Reimbursement as Reimbursement
From Reimbursement mt Inner Join
     Claim_6_2012 c1
     on c1.`Check Number` = mt.`Check Number` and mt.`Payment Date` = c1.`Payment Date`
Where mt.`Payment Date`>='2012-7-1' And mt.`Payment Date`<'2012-8-1'  

これはテーブルに結合されるため、支払日は引き続き使用できますが、値は NULL に設定されます。

于 2012-07-23T22:12:47.457 に答える