Remember Me
forgot your password?

GROUP BY IN XQUERY

XQuery is a query language that is designed to query collection of XML data. XQUERY provides a interface to manipulate data from XML or any other data source(relational). No doubt XQUERY is the faster interface to extract data from XML, as compared to read XML in dataest and read it. Reading XML data in dataset becomes pathetic when there is a huge amount of data in the XML file.  Inspite of the above fact, XQUERY is still not so much popular among developers. I tried to search GROUP BY statement in the XQUERY, I didn't find a single clue to perform it. Following is the Stored Procedure I had converted into XQUERY, and produce a GROUP BY and ORDER BY clauses with the loops & datset.   SQL STORED PROCEDURE:  

SELECT

 

Name,sum(Qty) as TotalQty FROM Customer

 

WHERE CustOrderID = @CustOrderID

GROUP BY Name

ORDER BY Name

Here I would get the Various Names of customer with their SUm of product Quantity.

Now, To convert the same in XQUERY I used the following Code:

VB.NET:

 

Dim dsCAQuanity As New DataSet()

Dim dsTemp As New DataSet

Dim dsCKT As New DataSet

Dim dtCKT As New DataTable

Dim drCKT As DataRow

Dim dcCkt(0) As DataColumn

dcCkt(0) = New DataColumn("Name", GetType(String))

dtCKT.Columns.Add(dcCkt(0))

dtCKT.PrimaryKey = dcCkt

dtCKT.Columns.Add("TotalQty", GetType(String))

query = "(document(""Doc"")//Table)[CustOrderID=""" & CustOrderID & """ ]"

dsCKT = xmlXqury.ReadXMLFile("C:XMLDatabaseCustomer", query)

 

For Each rowCkt As DataRow In dsCKT.Tables(0).Rows

query =

"LET $doc := (document(""Doc"")//Table)[Name='" & rowCkt("Name") & "' and CustOrderID=""" & CustOrderID & """ ] return <qty> { sum($doc/Qty)} </qty>"

dsTemp =

New DataSet

dsTemp = xmlXqury.ReadXMLFile("C:XMLDatabaseCustomer", query)

 

If dsTemp.Tables.Count > 0 Then

 

If dsTemp.Tables(0).Rows.Count > 0 And dtCKT.Rows.IndexOf(dtCKT.Rows.Find(rowCkt("Name").ToString)) < 0 Then

drCKT = dtCKT.NewRow

drCKT(

"Name") = rowCkt("Name")

drCKT(

"TotalQty") = dsTemp.Tables(0).Rows(0)(0)

dtCKT.Rows.Add(drCKT)

 

End If

 

End If

 

Next

dsCAQuanity =

New DataSet()

dsCAQuanity.Tables.Add(dtCKT)

 

C#:

 

    DataSet dsCAQuanity = new DataSet();

    DataSet dsTemp = new DataSet();

    DataSet dsCKT = new DataSet();

    DataTable dtCKT = new DataTable();

    DataRow drCKT = default(DataRow);

    DataColumn[] dcCkt = new DataColumn[1];

     dcCkt(0) = new DataColumn("Name", typeof(string));

        dtCKT.Columns.Add(dcCkt(0));

        dtCKT.PrimaryKey = dcCkt;

        dtCKT.Columns.Add("TotalQty", typeof(string));

        query = "(document("Doc")//Table)[CustOrderID="" + CustOrderID + "" ]";

        dsCKT = xmlXqury.ReadXMLFile("C:XMLDatabaseCustomer", query);

        foreach (DataRow rowCkt in dsCKT.Tables(0).Rows) {

                query = "LET $doc := (document("Doc")//Table)[Name='" + rowCkt("Name") + "' and CustOrderID="" + CustOrderID + "" ] return <qty> { sum($doc/Qty)} </qty>";

               dsTemp = new DataSet();

               dsTemp = xmlXqury.ReadXMLFile("C:XMLDatabaseCustomer", query);

                if (dsTemp.Tables.Count > 0) {

                        if (dsTemp.Tables(0).Rows.Count > 0 & dtCKT.Rows.IndexOf(dtCKT.Rows.Find(rowCkt("Name").ToString)) < 0) {

                                drCKT = dtCKT.NewRow;

                                drCKT("Name") = rowCkt("Name");

                                drCKT("TotalQty") = dsTemp.Tables(0).Rows(0)(0);

                              

                dtCKT.Rows.Add(drCKT);

                          }

                    }

    }

        dsCAQuanity = new DataSet();

        dsCAQuanity.Tables.Add(dtCKT);

 

The data set dsCAQuanity will return the same record as given by the above stored procedure, means it will reterive the record with GROUP BY and ORDER BY clause.

Hope it will sort out your illusion for XQUERY group BY & order by statement. XQUERY provides Order by clause, whcih can also be used with different aspects.

 

Happy Coding.

Anuj Tripathi

Anuj Tripathi Mothers are those wonderful people who can get up in the morning before the smell of coffee.

Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Programming Articles
  • More from Anuj Tripathi

The ASP Gauge: More Than The Usual

By: chaudhary fahim | 20/11/2009
Digital dashboards often have several displayed gauges showcasing different data being monitored. An ASP gauge is one such type of gauge used on an ASP template page. Digital dashboards are tools which show data being monitored for easy referencing and analysis. In this fast paced world, there is a need for such tools in order to manage huge amounts of data simultaneously.

Drivers Installation With WiX

By: Apriorit | 20/11/2009
This article is devoted to the methods of the driver packages installation/uninstallation using WiX. In particular we will discuss the usage of the Microsoft DPInst utility for the installation and removal of the drivers during the Windows Installer package setup, review the principles of DPInst functioning, review the issues with the unsigned drivers installation and learn how to launch multiple MSI installations from already running Windows Installer package setup.

Database design & development in Orange County

By: Mike Nielson | 20/11/2009
The database development solutions are designed to provide the integrity of data along with the security of applications. The database systems are required for effective storage of data and management of data so that the information may be utilized in the software applications. Database development takes place in phases, such as analysis performed by the business analysts based on the requirements collected from clients.

Open Source Application: Distribution Terms

By: Tyler Moon | 20/11/2009
Open source does not only mean accessing the source code. The distribution terms of open-source software must be in terms and agreement with the source code, free redistribution, derived works etc.

Developing a ColdFusion eCommerce Website Using a Single Database for Multiple Domains with Different Designs Saves Time and Money

By: Jason Luciano | 19/11/2009
This article outlines the concept of using Adobe ColdFusion and a single database to dynamically support six or more different website domains with different graphical user interfaces and designs. On an annual basis this project saved the client a countless amount of time and thousands of dollars all while building tremendous market share and product branding recognition simultaneously for all of their websites.

Know about Agile Methodology

By: Pavan Gorakavi | 19/11/2009
Agile denote nimbleness. Agile is a light weighted development methodology which is based on iterative development, where solutions evolve from tightly collaborated cross functional teams.

Contrastive analysis of methodologies of test design for functional testing

By: Apriorit | 18/11/2009
This article is for those who write or are going to write functional tests for any programs. The goal of this article is to give the flavor of the main techniques of test design and also to answer the question: What method is better to choose for testing your project?

How to put MP4 on iPod?

By: lazerer | 18/11/2009
How to put MP4 on iPod? This article will help you convert Video to MP4, put MP4 on iPod, set playlist on iPod, and tell you how to do when iPod MP4 file has no video.

GROUP BY IN XQUERY

By: Anuj Tripathi | 13/11/2009 | Programming
This article will provide you to implement GROUP BY clause in XQUERY with the code.

Creating Sql Database Connection in Asp.net Web Application Through C#

By: Anuj Tripathi | 20/12/2008 | Programming
Following I am mentioning step-by-step procedure to create database connection to the asp.net website textboxes via C#.

C++ Code for Bus Reservatio System

By: Anuj Tripathi | 20/12/2008 | Programming
Following code is for Bus reservation system. Coding Done in C++. Just copy paste this code in a text file.Name it bus.cpp, and save this file in your "Turbo C BIN folder". Also You can use Dev C++ and other compilers for C++. Comment it if u like it. If still you have doubt, feel free to ask. Thanks

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (1.13, 2, w2)