2016-06-24 10 views
-1

テーブルの1つに行が表示されない。下の表の例では、今月(6月)にtbl_loanledgerにトランザクションがないテーブルtbl_loan_masterの行を出力したいとします。私はすでに現在の月(6月)内の各借り手の最後の取引を返しますが、tbl_loan_masterテーブルでは返さない既存のクエリを既に持っています。削除された列は、値が1の場合は削除され、0の場合は削除されます。もう一方のテーブルに存在しない行をフェッチする

tbl_borrowers

---------------------------------------------------------- 
| id | first_name | last_name | deleted | date_created | 
---------------------------------------------------------- 
| 1 | Bill | Snow | 0  | 2016/04/08 | 
| 2 | Meg  | Rib  | 0  | 2016/04/13 | 
| 3 | Yin  | Ling | 0  | 2016/05/17 | 
| 4 | Sam  | Taylor | 0  | 2016/05/17 | 
| 5 | Bob  | Canny | 1  | 2016/05/25 | 
| 6 | Drake | Fig  | 0  | 2016/05/28 | 
| 7 | May  | Mcday | 0  | 2016/05/28 | 
---------------------------------------------------------- 

tbl_loan_master

---------------------------------------------------------------------- 
| id | borrower_id | loan | date_created | due_date | deleted | 
---------------------------------------------------------------------- 
| 1 |  4  | 300 | 2016/04/28 | 2017/04/28 |  0  | 
| 2 |  1  | 100 | 2016/05/05 | 2017/05/05 |  0  | 
| 3 |  2  | 500 | 2016/06/08 | 2017/06/08 |  0  | 
| 4 |  1  | 200 | 2016/06/13 | 2017/06/13 |  0  | 
| 5 |  3  | 150 | 2016/06/15 | 2017/06/15 |  0  | 
| 6 |  6  | 50 | 2016/06/16 | 2017/06/16 |  0  | 
| 7 |  1  | 100 | 2016/06/20 | 2017/06/20 |  1  | 
---------------------------------------------------------------------- 

tbl_loanledger

--------------------------------------------------------------------------------- 
| id | borrower_id | loanmaster_id | payment | balance| date_created | deleted | 
--------------------------------------------------------------------------------- 
| 1 |  4  |  1  | 50 | 250 | 2016/05/28 | 0 | 
| 2 |  1  |  2  | 20 | 80 | 2016/05/25 | 0 | 
| 3 |  1  |  2  | 30 | 50 | 2016/06/01 | 0 | 
| 4 |  2  |  3  | 100 | 400 | 2016/06/09 | 0 | 
| 5 |  2  |  3  | 50 | 350 | 2016/06/10 | 0 | 
| 6 |  1  |  4  | 50 | 200 | 2016/06/16 | 0 | 
| 7 |  1  |  7  | 50 | 250 | 2016/06/16 | 1 | 
--------------------------------------------------------------------------------- 

結果は、これは私がどんな助けが高く評価され、このいずれかに苦労していたクエリ

select t.first_name, t.last_name, t1.due_date, coalesce(t.loan_balance, t1.loan) as balance 
    from (
     select t1.*, t2.loan_balance, t2.date_created as d, t2.loanmaster_id, t2.id as tid 
     from ".tbl_borrowers." t1 
     left join ".tbl_loanledger." t2 
     on t1.id = t2.borrower_id 
     where t1.deleted = 0) t 
    left join (
     select t3.* 
     from ".tbl_loan_master." t3 
     inner join (select max(id) as id from ".tbl_loan_master." group by borrower_id) t4 on t3.id = t4.id 
      ) t1 on t.id = t1.borrower_id 
    where month(t.d) = month(now()) 
    and t.tid in (select max(id) from ".tbl_loanledger." group by borrower_id) 
    or t.d is null 
    order by t.last_name 

ある

--------------------------------------------------------- 
| last_name | first_name | balance or loan | due_date | 
--------------------------------------------------------- 
| Fig | Drake |  50  | 2017/06/16 | 
| Ling | Yin  |  150  | 2017/06/15 | 
| Rib | Meg  |  350  | 2017/06/08 | 
| Snow | Bill |  200  | 2017/06/13 | 
--------------------------------------------------------- 

を設定します。

+0

私はこの1つを手伝ってください。 。 。 – zen

+0

だから、リング、テイラー、イチジクは結果セットに入るはずです。元帳残高に取引がない場合は、tbl_loan_masterから得られるはずです。元帳に6月より前の取引があり、そのトランザクション作成日> tbl_master.date_created balanceがborrower_idの最後の元帳エントリから来るはずですか? –

答えて

0
/* 
CREATE table tbl_borrowers (id int,first_name varchar(10), last_name varchar(10), deleted int, date_created date); 
truncate table tbl_borrowers; 
insert into tbl_borrowers values 
( 1 , 'Bill' , 'Snow' ,  0  , '2016/04/08'), 
( 2 , 'Meg'  , 'Rib' ,  0  , '2016/04/13'), 
( 3 , 'Yin'  , 'Ling' ,  0  , '2016/05/17'), 
( 4 , 'Sam'  , 'Taylor' ,  0  , '2016/05/17'), 
( 5 , 'Bob'  , 'Canny' ,  1  , '2016/05/25'), 
( 6 , 'Drake' , 'Fig' ,  0  , '2016/05/28'), 
( 7 , 'May'  , 'Mcday' ,  0  , '2016/05/28') ; 
drop table tbl_loan_master; 
create table tbl_loan_master(id int,borrower_id int,loan int,date_created date,due_date date,deleted int); 
truncate table tbl_loan_master; 
insert into tbl_loan_master values 
( 1 ,  4  , 300 , '2016/04/28' , '2017/04/28' ,  0 ), 
( 2 ,  1  , 100 , '2016/05/05' , '2017/05/05' ,  0 ), 
( 3 ,  2  , 500 , '2016/06/08' , '2017/06/08' ,  0 ), 
( 4 ,  1  , 200 , '2016/06/13' , '2017/06/13' ,  0 ), 
( 5 ,  3  , 150 , '2016/06/15' , '2017/06/15' ,  0 ), 
( 6 ,  6  , 50 , '2016/06/16' , '2017/06/16' ,  0 ), 
( 7 ,  1  , 100 , '2016/06/20' , '2017/06/20' ,  1 ) ; 

create table tbl_loanledger(id int,borrower_id int,loanmaster_id int,payment int,balance int,date_created date,deleted int); 
truncate table tbl_loanledger; 
insert into tbl_loanledger values 
( 1 ,  4  ,  1  , 50 , 250 , '2016/05/28' , 0 ), 
( 2 ,  1  ,  2  , 20 , 80 , '2016/05/25' , 0 ), 
( 3 ,  1  ,  2  , 30 , 50 , '2016/06/01' , 0 ), 
( 4 ,  2  ,  3  , 100 , 400 , '2016/06/09' , 0 ), 
( 5 ,  2  ,  3  , 50 , 350 , '2016/06/10' , 0 ), 
( 6 ,  1  ,  4  , 50 , 200 , '2016/06/16' , 0 ), 
( 7 ,  1  ,  7  , 50 , 250 , '2016/06/16' , 1 ) ; 
*/ 
select t.last_name,t.first_name,t.borrower_id, 
      case  
       when t.lldatecreated is null then t.loan 
       else t.balance 
       end as 'Loan or Balance' 
       ,t.lmdatecreated 
       ,t.due_date 
       ,t.lldatecreated 
from  
(
select b.last_name,b.first_name,lm.borrower_id,lm.loan ,lm.date_created as lmdatecreated, lm.due_date,s.balance,s.lldatecreated 
from tbl_loan_master lm 
join  tbl_borrowers b on b.id = lm.borrower_id 
left outer join 
(
select ll.borrower_id ,ll.date_created as lldatecreated,ll.balance 
from tbl_loanledger ll 
where  ll.id = (
         select  max(ll1.id) 
         from tbl_loanledger ll1 
         where  ll1.borrower_id = ll.borrower_id 
         and ll1.deleted <> 1 
         #and year(ll1.date_created) * 12 + month(ll1.date_created) = year(now()) * 12 + month(now()) 
         ) 
) s on lm.borrower_id = s.borrower_id 
where  lm.id = (
         select  max(lml.id) 
         from tbl_loan_master lml 
         where  lml.borrower_id = lm.borrower_id 
         and deleted <> 1 
         ) 
       and b.deleted <> 1 
) t 
where  t.lldatecreated is null or 
      (
      year(t.lldatecreated) * 12 + month(t.lldatecreated) < year(now()) * 12 + month(now()) 
      and t.lldatecreated >= t.lmdatecreated 
      ) 
; 
関連する問題