Select MAX and RIGHT OUTER JOIN

koc

I have platform to extract data from sql tables and so far all queries were generated by simple drag and drop tool. Now I am trying to change query manually, but it's not working as expected...

Can you take a look?

Query delivered by generator:

SELECT
  repo.MAT.MAT_A_COD,
  inventory.INV.MRP_RQMT_DT,
  SUM(inventory.INV.MRP_AVL_QTY)
  
FROM
  repo.MAT RIGHT OUTER JOIN inventory.INV ON (inventory.INV.MRP_MAT_A_FK=repo.MAT.MAT_A_PK)
  
WHERE
  ( inventory.INV.MRP_COMPANY_COD IN ('01','02')  )
  
GROUP BY
  1, 
  2

Results:

Material A | 2020.01.01 | 100
Material A | 2020.01.02 | 200
Material A | 2020.01.03 | 300
Material B | 2020.01.01 | 10
Material B | 2020.01.02 | 0

What I am looking for: only values for the latest date for each material.

Material A | 2020.01.03 | 300
Material B | 2020.01.02 | 0

I tried with MAX(inventory.INV.MRP_RQMT_DT), but no success. Any help is appreciated!

Fahmi

You can try the below -

SELECT
  repo.MAT.MAT_A_COD,
  inventory.INV.MRP_RQMT_DT,
  SUM(inventory.INV.MRP_AVL_QTY)
FROM
  repo.MAT RIGHT OUTER JOIN inventory.INV ON inventory.INV.MRP_MAT_A_FK=repo.MAT.MAT_A_PK
WHERE
  inventory.INV.MRP_COMPANY_COD IN ('01','02') and inventory.INV.MRP_RQMT_DT=(select max(inventory.INV.MRP_RQMT_DT) from inventory.INV inv1 where inventory.INV.MRP_MAT_A_FK=inv1.MRP_MAT_A_FK)
GROUP BY 1, 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

HQL right outer join

From Dev

Right Outer Join to Left Outer join

From Dev

full outer join or right outer join

From Dev

outer join to select

From Dev

LINQ to SQL Right Outer Join

From Dev

Linq to object Right outer join

From Dev

LINQ to SQL Right Outer Join

From Dev

MySQL right outer join query

From Dev

how to write combination of RIGHT OUTER JOIN and LEFT OUTER JOIN

From Dev

select from LEFT OUTER JOIN

From Dev

Right JOIN with select result

From Dev

How to do full right outer join in kdb?

From Dev

Perform right outer join with a condition for left table

From Dev

Right outer join with multiple where conditions

From Dev

Right outer join with derived table in MS Access

From Dev

Performing a right outer join with a group by using linq

From Dev

LINQ: How to perform a RIGHT OUTER JOIN?

From Dev

Right outer join in linq to entities query

From Dev

1 Outer Join vs Left join following with Right Join

From Dev

1 Outer Join vs Left join following with Right Join

From Dev

Challenging LEFT OUTER JOIN query grouping by MAX

From Dev

Two right joins / right + outer join, etc not working

From Dev

When doing an OUTER JOIN to a table how can I join in another table to the one on the Right of the outer Join?

From Dev

When doing an OUTER JOIN to a table how can I join in another table to the one on the Right of the outer Join?

From Dev

Select query with left outer join and sum with group by

From Dev

Select using LEFT OUTER JOIN with condition

From Dev

Select rows with Left Outer Join and condition - MySQL

From Dev

Old Style Oracle Outer Join Syntax - Why locate the (+) on the right side of the equals sign in a Left Outer join?

From Dev

MySQL - how do I do an outer join where either side is optional - left and right outer join

Related Related

  1. 1

    HQL right outer join

  2. 2

    Right Outer Join to Left Outer join

  3. 3

    full outer join or right outer join

  4. 4

    outer join to select

  5. 5

    LINQ to SQL Right Outer Join

  6. 6

    Linq to object Right outer join

  7. 7

    LINQ to SQL Right Outer Join

  8. 8

    MySQL right outer join query

  9. 9

    how to write combination of RIGHT OUTER JOIN and LEFT OUTER JOIN

  10. 10

    select from LEFT OUTER JOIN

  11. 11

    Right JOIN with select result

  12. 12

    How to do full right outer join in kdb?

  13. 13

    Perform right outer join with a condition for left table

  14. 14

    Right outer join with multiple where conditions

  15. 15

    Right outer join with derived table in MS Access

  16. 16

    Performing a right outer join with a group by using linq

  17. 17

    LINQ: How to perform a RIGHT OUTER JOIN?

  18. 18

    Right outer join in linq to entities query

  19. 19

    1 Outer Join vs Left join following with Right Join

  20. 20

    1 Outer Join vs Left join following with Right Join

  21. 21

    Challenging LEFT OUTER JOIN query grouping by MAX

  22. 22

    Two right joins / right + outer join, etc not working

  23. 23

    When doing an OUTER JOIN to a table how can I join in another table to the one on the Right of the outer Join?

  24. 24

    When doing an OUTER JOIN to a table how can I join in another table to the one on the Right of the outer Join?

  25. 25

    Select query with left outer join and sum with group by

  26. 26

    Select using LEFT OUTER JOIN with condition

  27. 27

    Select rows with Left Outer Join and condition - MySQL

  28. 28

    Old Style Oracle Outer Join Syntax - Why locate the (+) on the right side of the equals sign in a Left Outer join?

  29. 29

    MySQL - how do I do an outer join where either side is optional - left and right outer join

HotTag

Archive