Thread: Count Number of Groups


Permlink Replies: 23 - Pages: 2 [ 1 2 | Next ] - Last Post: Jan 29, 2008 11:34 AM Last Post By: user504214
nagornyi

Posts: 811
Registered: 03/30/01
Count Number of Groups
Posted: Nov 19, 2007 9:23 AM
Click to report abuse...   Click to reply to this thread Reply
Here is sample XML:
<ROWSET>
 <ROW>
  <DEPT>A1</DEPT> 
  <DATA>2112</DATA> 
  </ROW>
 <ROW>
  <DEPT>A1</DEPT> 
  <DATA>7985</DATA> 
  </ROW>
 <ROW>
  <DEPT>B1</DEPT> 
  <DATA>8452</DATA> 
  </ROW>
 <ROW>
  <DEPT>C2</DEPT> 
  <DATA>2159</DATA> 
  </ROW>
</ROWSET>

Using Table Wizard, I am getting this report:
A1     2112
       7985
B1     8452
C2     2159

Everything OK, but at the report header I need to count the number of DEPT, i.e. 3. How to get it? Thanks.
Tim Dexter

Posts: 2,268
Registered: 01/10/01
Re: Count Number of Groups
Posted: Nov 20, 2007 6:28 AM   in response to: nagornyi in response to: nagornyi
Click to report abuse...   Click to reply to this thread Reply
Just use the count function but provide the full path to the DEPT element

count(/ROWSET/ROW/DEPT)

a little long winded but its going to be much faster than

count(.//DEPT)

That will send the parser off searching for all occurrences throughout the XML
Tim
nagornyi

Posts: 811
Registered: 03/30/01
Re: Count Number of Groups
Posted: Nov 20, 2007 7:20 AM   in response to: Tim Dexter in response to: Tim Dexter
Click to report abuse...   Click to reply to this thread Reply
Both give count of 4, not 3.
Tim Dexter

Posts: 2,268
Registered: 01/10/01
Re: Count Number of Groups
Posted: Nov 20, 2007 9:05 AM   in response to: nagornyi in response to: nagornyi
Click to report abuse...   Click to reply to this thread Reply
Ahhhh ... you tricked me !
Tim Dexter

Posts: 2,268
Registered: 01/10/01
Re: Count Number of Groups
Posted: Nov 20, 2007 10:02 AM   in response to: nagornyi in response to: nagornyi
Click to report abuse...   Click to reply to this thread Reply
Hey Vadim
OK, I felt guilty for the crappy answer and we have a solution. I shared your question and an answer with the rest of the world.

http://blogs.oracle.com/xmlpublisher/2007/11/20#a679

Regards

Tim
nagornyi

Posts: 811
Registered: 03/30/01
Re: Count Number of Groups
Posted: Nov 20, 2007 11:21 AM   in response to: Tim Dexter in response to: Tim Dexter
Click to report abuse...   Click to reply to this thread Reply
Thank you, Tim, now it's counting correctly. The link that works for me though is thisL
http://blogs.oracle.com/xmlpublisher/2007/11/20
Neil M.

Posts: 112
Registered: 04/22/06
Re: Count Number of Groups
Posted: Nov 20, 2007 12:02 PM   in response to: Tim Dexter in response to: Tim Dexter
Click to report abuse...   Click to reply to this thread Reply
If your data is ordered, then you may simply count all ROWS that have a different DEPT than the previous one, keeping into account the first row of course. This might give you a better performance for large XML files.

<?count(ROW[position()=1 or ./preceding-sibling::ROW1]/DEPT!=DEPT)?>

XPATH 2.0 does provide something similar, but AFAIK XPATH 2.0 does not work in Oracle XMLP yet.

<?count(distinct-values('DEPT')?>

HTH,
Nilanshu.
nagornyi

Posts: 811
Registered: 03/30/01
Re: Count Number of Groups
Posted: Nov 20, 2007 1:43 PM   in response to: Neil M. in response to: Neil M.
Click to report abuse...   Click to reply to this thread Reply
Thank you, Nilansu, this also works fine.
May I suggest you to augment the Tim's blog entry with this solution?
Tim Dexter

Posts: 2,268
Registered: 01/10/01
Re: Count Number of Groups
Posted: Nov 20, 2007 2:21 PM   in response to: nagornyi in response to: nagornyi
Click to report abuse...   Click to reply to this thread Reply
Done
Kazeem

Posts: 4
Registered: 12/05/07
Re: Count Number of Groups
Posted: Dec 5, 2007 12:10 PM   in response to: Tim Dexter in response to: Tim Dexter
Click to report abuse...   Click to reply to this thread Reply
Hi Tim,

These solutions are all great. However i have a unique situation. Using the ROWSET example above, how would you count the distinct number of DEPTs within a ROW. The solutions above provide the distinct count of DEPTs within ROWSET (the entire document) but i am trying to count the distinct number of DEPTs within a ROW. Any help will be deeply appreciated. Thanks
Vetsrini

Posts: 2,615
Registered: 07/24/06
Re: Count Number of Groups
Posted: Dec 5, 2007 3:48 PM   in response to: Kazeem in response to: Kazeem
Click to report abuse...   Click to reply to this thread Reply
Hi Kazeem,
The above example xml, contains,
lot of row within rowset,
and each row contains only one dept within the row.

but you are asking us
"count the distinct number of DEPTs within a ROW"

can you provide your xml here ? with row having more than one dept.
Kazeem

Posts: 4
Registered: 12/05/07
Re: Count Number of Groups
Posted: Dec 5, 2007 5:44 PM   in response to: Vetsrini in response to: Vetsrini
Click to report abuse...   Click to reply to this thread Reply
Thanks Vetsrini, below i am going to use the same but just modified as i think it will be easier to work with...

<ROWSET>
<ROW>
<DEPT>A1</DEPT>
<DEPT>A1</DEPT>
<DEPT>X1</DEPT>
<DEPT>Y1</DEPT>
<DEPT>Z1</DEPT>
<DATA>2112</DATA>
</ROW>
<ROW>
<DEPT>A1</DEPT>
<DATA>7985</DATA>
</ROW>
<ROW>
<DEPT>B1</DEPT>
<DATA>8452</DATA>
</ROW>
<ROW>
<DEPT>C2</DEPT>
<DATA>2159</DATA>
</ROW>
</ROWSET>

...as you can see the first ROW tag has 5 DEPT elements but only 4 distinct ones. The actual application of this is a for a Purchase Order with lines shipping to different locations. I need to be able to find out (for each purchase order) how many distinct locations goods are being shipped to. If you can help with this then i will apply same logic to my actual XML data. Thanks
Vetsrini

Posts: 2,615
Registered: 07/24/06
Re: Count Number of Groups
Posted: Dec 5, 2007 7:38 PM   in response to: Kazeem in response to: Kazeem
Click to report abuse...   Click to reply to this thread Reply
I am guessing , you are looping through the each PO
to identify all the unique locations,

similarly
<?for-each:ROW?>
<?count(xdoxslt:distinct_values(DEPT))?>
<end for-each>
<?count(xdoxslt:distinct_values(/ROWSET/ROW/DEPT))?>

4
1
1
1

6
Kazeem

Posts: 4
Registered: 12/05/07
Re: Count Number of Groups
Posted: Dec 9, 2007 4:45 PM   in response to: Vetsrini in response to: Vetsrini
Click to report abuse...   Click to reply to this thread Reply
Vetsrini,

Thanks for your post. I tried that already and it works except that i have a unique problem. Some Purchase Orders will not have a shipment tag at all hence i am getting a null pointer exception on those. To get the error i am getting, remove <DEPT>B1</DEPT> from the xml you are using to test. This will cause the 3rd ROW tag not to have a department and you will get a null pointer exception. Is there a way to handle this? Thanks
Vetsrini

Posts: 2,615
Registered: 07/24/06
Re: Count Number of Groups
Posted: Dec 10, 2007 7:07 AM   in response to: Kazeem in response to: Kazeem
Click to report abuse...   Click to reply to this thread Reply
try this

<?count(xdoxslt:distinct_values(DEPThttp://.!=’’))?>
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums