FLWOR の例
完成した照会の中で FLWOR 式を使用して、結合、グループ化、および集計を実行することができます。
XML データを結合する FLWOR 式
以下の照会では、XQuery FLWOR 式を使って結合を表します。
PURCHASEORDER 表の列 PORDER に、以下のデータが含まれているとします。
<ipo:purchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ipo="http://www.example.com/IPO"
orderDate="2008-12-01">
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Helen Zoe</name>
<street>55 Eden Street</street>
<city>San Jose</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Joe Lee</name>
<street>66 University Avenue</street>
<city>Palo Alto</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<billTo xsi:type="ipo:USAddress">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<items>
<item partNum="833-AA">
<productName>Lapis necklace</productName>
<quantity>1</quantity>
<USPrice>99.95</USPrice>
<ipo:comment>Want this for the holidays!</ipo:comment>
<shipDate>2008-12-05</shipDate>
</item>
<item partNum="945-ZG">
<productName>Sapphire Bracelet</productName>
<quantity>2</quantity>
<USPrice>178.99</USPrice>
<shipDate>2009-01-03</shipDate>
</item>
</items>
</ipo:purchaseOrder>
また、STATUS という表に含まれる STATUS という名前の XML 列に、以下のデータが含まれるとします。
<status>
<statusItem>
<name>Robert Smith</name>
<status>Premier</status>
<comment>Orders a lot of jewelry</comment>
<comment>Has friends in the Silicon Valley</comment>
</statusItem>
<statusItem>
<name>Jane Carmody</name>
<status>Unreliable</status>
<comment>Has unpaid bills</comment>
</statusItem>
</status>
以下の例は、Premier 状況を持つ顧客による購入オーダーを見つけるために XQuery FLWOR 式を使用する方法を示しています。
SELECT XMLQUERY(
'declare namespace ipo="http://www.example.com/IPO";
for $i in $po/ipo:purchaseOrder
return
<premierOrders> {
for $j in $status/status/statusItem
where $j/name=$i/billTo/name and $j/status="Premier"
return
$i }
</premierOrders>'
PASSING T1.PORDER as "po", T2.STATUS as "status")
FROM PURCHASEORDER T1, STATUS T2;
この照会は以下の結果を戻します。
<premierOrders>
<ipo:purchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ipo="http://www.example.com/IPO" orderDate="1999-12-01">
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Helen Zoe</name>
<street>55 Eden Street</street>
<city>San Jose</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Joe Lee</name>
<street>66 University Avenue</street>
<city>Palo Alto</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<billTo xsi:type="ipo:USAddress">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<items>
<item partNum="833-AA">
<productName>Lapis necklace</productName>
<quantity>1</quantity>
<USPrice>99.95</USPrice>
<ipo:comment>Want this for the holidays!</ipo:comment>
<shipDate>2008-12-05</shipDate>
</item>
<item partNum="945-ZG">
<productName>Sapphire Bracelet</productName>
<quantity>2</quantity>
<USPrice>178.99</USPrice>
<shipDate>2009-01-03</shipDate>
</item>
</items>
</ipo:purchaseOrder>
</premierOrders>
条件付きロジックを使用する FLWOR 式
以下の例は、既に配送された、PURCHASEORDER 表の PORDER 列の項目に関する収益レポートを構築するために、XQuery を使用する方法を示しています。
SELECT XMLQUERY(
'let $j := (
for $i in $po//item
return if (xs:date($i/shipDate) <= $currentDate)
then xs:decimal($i/USPrice)
else 0.0 )
return fn:sum($j)'
PASSING T1.PORDER as "po", CURRENT DATE as "currentDate")
FROM PURCHASEORDER T1;
現在の日付が 2009-01-01 であるとします。 この照会は以下の結果を戻します。
99.95