query to ignore duplicate/null records

前端 未结 3 474
夕颜
夕颜 2021-01-26 06:36

Below are the 3 tables which we need to join to get the data

 TableA         TableB               TableC
    AId          BId     Name      CId       DeclareDate         


        
相关标签:
3条回答
  • 2021-01-26 06:55

    If you mean to say DeclareDate is an actual date, and you only want the values from December, maybe something like this would work?

    select
      a.AId, b.BId, b.Name,
      coalesce (c.CId, A.AId) as CId,
      c.DeclareDate, c.Value
    from
      TableA a
      join TableB b on a.AId = b.BId
      left join TableC c on
        a.AId = c.CId and
        extract (month from c.DeclareDate) = 12
    

    This assumes a whole lot -- notably that if you have multiple dates in December, they will all show up. Also, it doesn't check the year... is any old December adequate?

    If you literally meant the text string "December" then you could change that second condition in the left join to c.DeclareDate = 'December'.

    If you have some broader examples, maybe it will help pin down the exact logic.

    -- EDIT --

    Here is some sample code to build and test:

    insert into tablea values (1);
    insert into tablea values (2);
    insert into tablea values (3);
    insert into tablea values (4);
    insert into tablea values (5);
    insert into tableb values (1, 'abc');
    insert into tableb values (2, 'def');
    insert into tableb values (3, 'xyz');
    insert into tableb values (4, 'pqr');
    insert into tableb values (5, 'ghi');
    insert into tablec values (1, 'September', 11);
    insert into tablec values (1, 'October', 12);
    insert into tablec values (1, 'November', 13);
    insert into tablec values (1, 'December', 14);
    insert into tablec values (2, 'September', 15);
    insert into tablec values (3, 'October', 16);
    insert into tablec values (4, 'August', 17);
    insert into tablec values (5, 'October', 18);
    insert into tablec values (5, 'December', 19);
    

    And the solution:

    select
      a.AId, b.BId, b.Name,
      coalesce (c.CId, A.AId) as CId,
      c.DeclareDate, c.Value
    from
      TableA a
      join TableB b on a.AId = b.BId
      left join TableC c on
        a.AId = c.CId and
        c.DeclareDate = 'December';
    

    Results:

    AID BID NAME  CID   DECLAREDATE VALUE
    1   1   abc   1     December    14
    2   2   def   2     
    3   3   xyz   3     
    4   4   pqr   4     
    5   5   ghi   5     December    19
    
    0 讨论(0)
  • 2021-01-26 06:56

    Try this one

    select a.AID,b.DeclareDate,b.VAL from TABLEA a left outer join (select * from TABLEB where DeclareDate='December') b on a.AID=b.BID order by a.AID asc

    0 讨论(0)
  • 2021-01-26 07:12

    use left join into table C

      select a.*,b.*, c.DeclareDate, c.Value from TableA a
       inner join TableB b on  a.AI =b.BId
      left join 
      (select * from TableC where DeclareDate='December') c
       on b.BId = c.CId
    

    OUTPUT

    0 讨论(0)
提交回复
热议问题