#  DC/MD_Q1 Return the customer id of the order that has matching id attribute value (1).

for $order in input()/order[@id="1"]
return
    $order/customer_id


# DC/MD_Q3 Group orders with total amount bigger than a certain number (11000.0), by customer id and calculate the total number of each group.

for $a in distinct-values (input()/order
    [total > 11000.0]/customer_id)
let $b := input()/order[customer_id=$a]
return
    <Output>
        <CustKey>{$a/text()}</CustKey>
        <NumberOfOrders>{count($b)}</NumberOfOrders>
    </Output>

# DC/MD_Q4 List the item id of the previous item of a matching item with id attribute value (8).

let $item := input()/items/:item[@id="8"]
for $prevItem in input()/items/:item
    [. << $item][position() = last()]
return
    <Output>
        <CurrentItem>{$item/@id}</CurrentItem>
        <PreviousItem>{$prevItem/@id}</PreviousItem>
    </Output>

# DC/MD_Q5 Return the first order line item of a certain order with id attribute value (2).

for $a in input()/order[@id="2"]
return
    $a/order_lines/order_line[1]


# DC/MD_Q6 Return invoice where some discount rates of sub-line items are higher than a certain number (0.02).

for $ord in input()/order
where some $item in $ord/order_lines/order_line
    satisfies $item/discount_rate gt 0.02
return
    $ord


# DC/MD_Q7 Return invoice where all discount rates of sub-line items are higher than a certain number (0.02).

for $ord in input()/order
where every $item in $ord/order_lines/order_line
    satisfies $item/discount_rate gt 0.02
return
    $ord


# DC/MD_Q8 Return the order line item ids of an order with an attribute value (3).

for $a in input()/order[@id="3"]
return
    $a/*/order_line/item_id


# DC/MD_Q9 Return the item ids of an order with id attribute value (4).

for $a in input()/order[@id="4"]
return
    $a//item_id


# DC/MD_Q10 List the orders (order id, order date and ship type), with total amount larger than a certain number (11000.0), ordered alphabetically by ship type.

for $a in input()/order
where $a/total gt 11000.0
order by $a/ship_type
return
    <Output>
        {$a/@id}
        {$a/order_date}
        {$a/ship_type}
    </Output>

# DC/MD_Q11 List the orders (order id, order date and order total), with total amount larger than a certain number (11000.0), in descending order by total amount.

for $a in input()/order
where $a/total gt 11000.0
order by $a/total descending
return
    <Output>
        {$a/@id}
        {$a/order_date}
        {$a/total}
    </Output>

# DC/MD_Q12 List all order lines of a certain order with id attribute value (5).

for $a in input()/order[@id="5"]
return
    <Output>
        {$a/order_lines}
    </Output>

# DC/MD_Q14 List the ids of orders that only have one order line.

for $a in input()/order
where empty($a/order_lines/order_line[2])
return
    <OneItemLine>
        {$a/@id}
    </OneItemLine>

# DC/MD_Q16 Retrieve one whole order document with certain id attribute value (6).

for $a in input()/order[@id="6"]
return
    $a


# DC/MD_Q17 Return the ids of authors whose biographies contain a certain word (``hockey").

for $a in input()/authors/author
where contains ($a/biography, "hockey")
return
<Output>
    {$a/@id}
</Output>

# DC/MD_19 For a particular order with id attribute value (7), get its customer name and phone, and its order status.

for $order in input()/order,
    $cust in input()/customers/customer
where $order/customer_id = $cust/@id
    and $order/@id = "7"
return
    <Output>
        {$order/@id}
        {$order/order_status}
        {$cust/first_name}
        {$cust/last_name}
        {$cust/phone_number}
    </Output>

