Loading TOC...

MarkLogic Server 11.0 Product Documentation
op:join-left-outer

op:join-left-outer(
   $leftPlan as map:map,
   $rightPlan as map:map,
   [$keys as map:map*],
   [$condition as map:map?]
) as map:map

Summary

This method yields one output row set with the rows from an inner join as well as the other rows from the left row set.

The join performs natural joins between columns with the same identifiers. To prevent inadvertent natural joins, specify a different qualifier for the left or right columns or use different column names for the left and right columns.

Parameters
$leftPlan The row set from the left view.
$rightPlan The row set from the right view.
$keys Equality condition(s) expressed using one or more calls to the function op:on. These conditions are used to compare the left and right rows.
$condition A boolean expression used to compare the left and right rows. See Boolean Expression Functions for the list of functions used to build boolean expressions.

Usage Notes

Both keys and condition are used to specify the join condition. keys allows for a simplified syntax for equality join condition. While condition allows for inequality and complex join conditions. This method combines keys and condition using an AND operator such that:
      $view
        => op:join-left-outer (
          $other-view,
          op:on(op:view-col('view', 'column1'), op:view-col('other-view', 'column2')),
          op:ge(op:view-col('view', 'column3'), op:view-col('other-view', 'column4'))
        )
is the same as
      $view
        => op:join-left-outer (
          $other-view,
          (),
          op:and(
            op:eq(op:view-col('view', 'column1'), op:view-col('other-view', 'column2')),
            op:ge(op:view-col('view', 'column3'), op:view-col('other-view', 'column4'))
          )
        )

Example

xquery version "1.0-ml";

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $employees := op:from-view("main", "employees")
let $expenses  := op:from-view("main", "expenses")
let $totalexpenses  := op:col("totalexpenses")
return $employees
   => op:join-left-outer($expenses, op:on(
                  op:view-col("employees", "EmployeeID"),
                  op:view-col("expenses", "EmployeeID")))
   => op:group-by(op:view-col("employees", "EmployeeID"),
                 ("FirstName", "LastName",
                  op:view-col("expenses", "Category"),
                  op:sum($totalexpenses,
                  op:view-col("expenses", "Amount"))))
   => op:order-by(op:view-col("employees", "EmployeeID"))

   => op:result()
  

Example

xquery version "1.0-ml";
 
import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

let $fruits := 
  op:from-literals((
    map:entry("fruit", "mango") => map:with("count", 10),
    map:entry("fruit", "apple") => map:with("count", 7),
    map:entry("fruit", "orange") => map:with("count", 9)
  ))
let $orders :=
  op:from-literals((
    map:entry("flavor", "apple") => map:with("cups", 5),
    map:entry("flavor", "orange") => map:with("cups", 14)
  ))
return $fruits
  => op:join-left-outer(
    $orders,
    op:on(op:col('fruit'), op:col('flavor')),
    op:ge(op:col('count'), op:col('cups'))
  )
  => op:result()

(: 
  fruit of 'orange' and 'mango' will pair with flavor of 'null' 
  fruit of 'apple' will pair with flavor of 'apple' 
:)
    

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.