Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4343052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:41:54+00:00 2026-05-21T11:41:54+00:00

My Requirement is to write a sql query to get the sub-region wise (fault)events

  • 0

My Requirement is to write a sql query to get the sub-region wise (fault)events count that occurred for the managedobjects. My database is postgres 8.4. Let me explain using the table structure.

My tables in django:
Managedobject:

class Managedobject(models.Model):
   name                = models.CharField(max_length=200, unique=True)
   iscontainer         = models.BooleanField(default=False,)
   parentkey           = models.ForeignKey('self', null=True)

Event Table:

class Event(models.Model):
    Name        = models.CharField(verbose_name=_('Name'))
    foid        = models.ForeignKey(Managedobject)

Managedobject Records:

NOC
   Chennai
      MO_1
      MO_2
      MO_3
   Mumbai
      MO_4
      MO_5
      MO_6
   Delhi
   Bangalore
IP
   Calcutta
   Cochin

Events Records:

event1 MO_1
event2 MO_2
event3 MO_3
event4 MO_5
event5 MO_6    

Now I need to get the events count for all the sub-regions. For example,

for NOC region:
  Chennai - 3
  Mumbai - 2
  Delhi - 0
  Bangalore - 0

So far I am able to get the result in two different queries.

  1. Get the subregions.

    select id from managedobject where iscontainer = True and parentkey = 3489
    
  2. For each of the region (using for loop), get the count as follows:

    SELECT count(*) 
    from event ev 
    WHERE ev.foid 
    IN (
        WITH RECURSIVE q AS (
            SELECT h 
            FROM managedobject h 
            WHERE parentkey = 3489 
            UNION ALL 
            SELECT hi 
            FROM q 
            JOIN managedobject hi 
            ON hi.parentkey = (q.h).id 
        ) 
        SELECT (q.h).id FROM q
    )
    

Please help to combine the queries to make it a single query and for getting the top 5 regions. Since the query is difficult in django, I am going for a raw sql query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:41:54+00:00Added an answer on May 21, 2026 at 11:41 am

    I got the query:

    WITH RECURSIVE q AS ( 
      SELECT  h, 
              1 AS level, 
              id AS ckey, 
              displayname as dname 
      FROM managedobject h 
      WHERE parentkey = 3489  
        and logicalnode=True 
    
     UNION ALL 
    
     SELECT  hi, 
             q.level + 1 AS level, 
             ckey, 
             dname 
     FROM q 
       JOIN managedobject hi ON hi.parentkey = (q.h).id 
    ) 
    SELECT count(ckey) as ccount, 
           ckey, 
           dname 
    FROM q 
      JOIN event as ev on ev.foid_id = (q.h).id 
    GROUP BY ckey, dname 
    ORDER BY ccount DESC 
    LIMIT 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an SQL query that will order the results by a
The requirement is that the user can write an article, therefore I choose type
We currently have a 10 year old nasty, spaghetti-code-style SQL Server database that we
I am looking for a database that can I run SQL statements on without
My requirement is just to display a set of values retrieved from database on
I'm writing an NHibernate criteria that selects data supporting paging. I'm using the COUNT(*)
What is the best way to write the system requirement specification. Definition is given
I am creating an application in grails that should read from one database and
I have two tables that I want to use in my query, tblEmployee and
We write applications that send out automated email alerts using SMTP. We generally configure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.