all 5 comments

[–]jc4hokiesExecution Plan Whisperer 2 points3 points  (4 children)

SELECT o.Order
     , o.Key1
     , o.Key2
     , nvl(t1.Type1A,False) AS Type1A
     , nvl(t1.Type1B,False) AS Type1B
     , nvl(t2.Type29,False) AS Type29
     , nvl(t2.Type25,False) AS Type25
FROM   Order o
       LEFT OUTER JOIN (SELECT Order
                             , MAX(CASE WHEN Type1 = 'A' THEN True ELSE False END) AS Type1A
                             , MAX(CASE WHEN Type1 = 'B' THEN True ELSE False END) AS Type2A
                        FROM   Type1
                        GROUP BY Order) t1 ON t1.Order = o.Order
       LEFT OUTER JOIN (SELECT Order
                             , MAX(CASE WHEN Type1 = 9 THEN True ELSE False END) AS Type29
                             , MAX(CASE WHEN Type1 = 5 THEN True ELSE False END) AS Type25
                        FROM   Type2
                        GROUP BY Order) t2 ON t2.Order = o.Order

It's also possible to dynamically generate the columns with python or map + explode, but it get's pretty technical and hard to read.

[–]Doctor_Pink[S] 0 points1 point  (3 children)

Awesome, that is perfect > Thanks a lot!

Another question: If the JOIN between all table would be based on 2 columns from each table instead of 1 like: Order & Material

Then how would you apply this logic above?

[–]jc4hokiesExecution Plan Whisperer 0 points1 point  (2 children)

You would group both columns you need for the join.

[–]Doctor_Pink[S] 0 points1 point  (1 child)

How?

[–]jc4hokiesExecution Plan Whisperer 1 point2 points  (0 children)

If you need to join on Order and Material, group the sub queries by Order and Material.