This page last changed on Mar 11, 2008.
eDocs Home > BEA AquaLogic Data Services Platform Documentation > Data Services Developer's Guide > Contents How to Create a Logical Data Service with a Group By ClauseThis topic shows how to add a group by clause to a logical data service, using the BEA extensions to XQuery.
OverviewIn relational data sources, a SQL GROUP BY statement is used with aggregate functions to group retrieved data by one or more columns. If you want to retrieve a list of distinct customers and the total amount of all orders each customer has placed from a relational data source, you might use a SQL statement like this: SELECT CUSTOMER_ID, SUM(TOTAL_ORDER_AMOUNT) FROM ORDERS GROUP BY CUSTOMER_ID The output produced groups all orders by customer and then totals the order amounts for each:
ALDSP logical data services use XQuery 1.0 to query data. XQuery, as defined by the W3C standard, does not support group by clauses. However, ALDSP has extended XQuery to allow a group by clause in an XQuery FLWOR statement: declare function tns:read() as element(ord1:ORDER_GROUP_BY)*{ for $CUSTOMER_ORDER in cus:CUSTOMER_ORDER() group $CUSTOMER_ORDER as $CUSTOMER_ORDER_group by $CUSTOMER_ORDER/CUSTOMER_ID as $CUSTOMER_ID_group return ... You can add the XQuery group by statement to a logical data service visually in Studio. You should first make sure the service has a return type that supports the group by. Suppose that after you retrieve all customer orders, group them by customer, and find the total amount of all orders each customer has placed, you also want a list of order IDs for each customer. You can design a logical data service to do this, doing part of the work in the mapping editor (in Studio) and part in the XQuery source. Design the Return Type SchemaThe return type schema needs an element to group by, such as a customer ID, and an element to hold an aggregate value, such as a sum or an average. The return type can also have a complex element that contains additional elements that provide information. This example provides the list of order IDs that are totalled for each customer, as one element with multiple cardinality within a complex element. Return Type Schema for a Group ByIf you want to design the schema top down using an XML editor, you can start with code like this and refactor it for your use case: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="ld:logical/OrderGroupBy"> <xs:element name="ORDER_GROUP_BY"> <xs:complexType> <xs:sequence> <xs:element name="CUSTOMER_ID" type="xs:string"/> <xs:element name="TOTAL_FOR_THIS_CUSTOMER" type="xs:decimal"/> <xs:element name="ORDERS"> <xs:complexType> <xs:sequence> <xs:element name="ORDER_ID" type="xs:string" maxOccurs="unbounded" form="unqualified" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> You can also create the return type bottom up, as you design the query map (see Create Your First Data Services).
Create the Logical Data ServiceOnce you have defined the return type, create the logical data service and add the group by statement visually, using the mapping editor.
At this point, do not draw additional mapping lines from the For block to the return type.
Create the Group By NodeNow create the group by node visually:
Create the For NodeTo map the information element, edit the XQuery code in the Source tab.
Add an Aggregate FunctionLast, add an aggregate function to the aggregate element in the return type (here, TOTAL_FOR_THIS_CUSTOMER).
Test the ServiceThe only way to test a logical data service with a group clause is to run the primary Read function in the Test tab. This type of data service does not have an update map, so you cannot edit data and submit it or test an Update procedure. Likewise, you cannot test a Create procedure.
You should see data grouped by the grouping element, with a result for the aggregate element, and containing a number of information elements. Results of a Group By StatementSee AlsoExamplesOther Resources
|
![]() |
Document generated by Confluence on Apr 28, 2008 15:54 |