Aggregation : Aggregate « XQuery « XML

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XQuery » Aggregate 
Aggregation


File: Data.xml

<employees>
  <employee name="James" salary="1234" yearServed="1" location="North"/>
  <employee name="Joe"   salary="3211" yearServed="2" location="South"/>
  <employee name="Jod"   salary="3422" yearServed="3" location="North"/>
  <employee name="Jason" salary="1321" yearServed="4" location="East"/>
  <employee name="Jack"  salary="2324" yearServed="3" location="North"/>
  <employee name="Jeep"  salary="4321" yearServed="2" location="East"/>
  <employee name="Jane"  salary="2344" yearServed="1" location="South"/>
</employees>

File: Query.xquery

for $d in distinct-values(doc("Data.xml")//employee/@location)
let $employees := doc("Data.xml")//employee[@location = $d]
order by $d
return <department code="{$d}"
                   numemployees="{count($employees)}"
                   distinct="{count(distinct-values($employees/@yearServed))}"
                   total="{sum($employees/@yearServed)}"/>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<department total="6" code="East" distinct="2" numemployees="2"/>
<department total="7" code="North" distinct="2" numemployees="3"/>
<department total="3" code="South" distinct="2" numemployees="2"/>

 
Related examples in the same category
1. Aggregating values
2. Aggregation on multiple values
3. Constraining and sorting on aggregated values
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.