Grouping

If you are used to SQL, then you might have been wondering what the equivalent to its DISTINCT and GROUP BY keywords is in XQuery FLOWR expressions. Well, SQL does not have one.

You can, however, get a fair amount of mileage from the distinct-values() function. Here is a query that groups videos according to who directed them:


              <movies>
               

                 {for $d in distinct-values(//director) return
               

                    <director name="{$d}">
               

                    { for $v in //video[director = $d] return
               

                       <title>{$v/title}</title>
               

                    }
               

                    </director>
               

                 }
               

              </movies>
               

            

This is not an ideal solution: apart from anything else, it depends heavily on the ability of the query processor to optimize the two nested loops to give good performance. But for the time being, this is all there is. This is an area where vendors are very likely to offer extensions to the language as defined by W3C.

Grouping was a notoriously weak point of XSLT 1.0, and the problem has been addressed with considerable success in the 2.0 version of the language. XQuery will likely follow suit.

XML Videos

Stylus Studio online XML video demonstrations illustrate how easy it is to learn advanced XML editing techniques. Now playing: Java Code Generation, Convert to XML, Grid View, and many other informative video demonstrations.

ETL - Extract, Transform, Load

Designing Extract, Load and Tranform (ETL) applications including ETL data warehousing, etl design and etl programming is easy with Stylus Studio's XML and ETL Tools. Start ETL Programming today.

Improving XSLT and XQuery Performance with Stylus Studio

This online video demonstration covers XQuery and XSLT performance tuning using the Stylus Studio XSLT profiler and the XQuery profiler.

XQuery Talk Blog

Get the latest XQuery news and information at XQuery-Talk - an online community for discussing XQuery technologies, examples, implementations and more. Bookmark this RSS or ATOM feed now!

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member