Pervasive
Sign in | Join | Help
in

Simplest way to retrieve category and a subcategory count from the same table in one statement

Last post 06-17-2008 8:49 AM by jpruneda. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-12-2008 7:01 PM

    • jimbo_k
    • Not Ranked
    • Joined on 09-10-2003
    • New England
    • Posts 3

    Simplest way to retrieve category and a subcategory count from the same table in one statement

    Dear friends and neighbors,

    I'm running Pervasive 9.5 (9.50.077.002) on a Microsoft Windows Server 2003 R2 Standard Edition with Service Pack 2.

    I have a table that I use to define a group of categories and subcategories.  This table is used to create nested menu options in a drill-down fashion.  Here is a simplified version for demonstation purposes.

    Table name : categories

    CatNum ParentNum CatName
    ------ --------- -----------
    1      0         Autos
    2      0         Motorcycles
    3      0         Trucks
    4      1         Audi
    5      1         Chevy
    6      1         Toyota
    7      2         Honda
    8      2         Kawasaki 
    9      2         Suzuki
    10     2         Yamaha
    11     3         Mack
    12     3         Volvo
    13     6         Camry
    14     6         Corolla
    15     6         Prius
    16     6         Yaris

    I want to write a query to produce the following:

    CatNum CatName     NumberOfSubcategories
    ------ ----------- ---------------------
    1      Autos       3
    2      Motorcycles 4
    3      Trucks      2
    4      Audi        0
    5      Chevy       0
    6      Toyota      4
    7      Honda       0
    8      Kawasaki    0
    9      Suzuki      0
    10     Yamaha      0
    11     Mack        0
    12     Volvo       0
    13     Camry       0
    14     Corolla     0
    15     Prius       0
    16     Yaris       0

    I want to produce the above results in one query. This will eventually be deployed on an ASP page for an intranet.

    To prototype the SQL, I've tried various methods that work in Microsoft Access, but when I try them in the Pervasive Control Center, I get a SQL Execution Error (java.io.IOException: LNA session closed). Then I need to restart the Pervasive services on the server. In-line techniques all seem to fail.

    The only method that succeeds is as follows:

    SELECT c1.CatNum, c1.CatName,
       ( SELECT Count(c2.ParentNum)
         FROM categories c2
         WHERE c2.ParentNum = c1.CatNum ) AS NumberOfSubcategories
    FROM categories c1

    Is this the best way to produce the information I desire?

  • 06-12-2008 10:02 PM In reply to

    Re: Simplest way to retrieve category and a subcategory count from the same table in one statement

    If you have queries that are causing the system to crash, then you may be best off submitting a formal incident, or at least a defect report, and providing the DDF's, data files, and faulty queries.  You should NOT be able to write a query that crashes the engine! 

    Bill Bach
    Goldstar Software Inc.
    www.goldstarsoftware.com
  • 06-17-2008 8:49 AM In reply to

    Re: Simplest way to retrieve category and a subcategory count from the same table in one statement

    Prior to submitting an incident, you might want to try applying update 61 to see if the problem still exists http://ww2.pervasive.com/Database/MaintenanceReleases/Pages/PervasivePSQLv9ServicePack2(95).aspx

    If the problem exists, please open an ticket.

    Jose Pruneda

    Pervasive Software.

     

     

Page 1 of 1 (3 items)
© 2008 Pervasive Software Inc. All Rights Reserved.