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

  • SEARCH
  • Home
  • 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 8589031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:56:48+00:00 2026-06-11T22:56:48+00:00

I have a relatively large (as in >10^6 entries) table called things which represent

  • 0

I have a relatively large (as in >10^6 entries) table called “things” which represent locateable objects, e.g. countries, areas, cities, streets, etc. They are used as a tree of objects with a fixed depth, so the table structure looks like this:

id
name
type
continent_id
country_id
city_id
area_id
street_id
etc.

The association inside “things” is 1:n, i.e. a street or area always belongs to a defined city and country (not two or none); the column city_id for example contains the id of the “city” thing for all the objects which are inside that city. The “type” column contains the type of thing (Street, City, etc) as a string.

This table is referenced in another table “actions” as “thing_id”. I am trying to generate a table of action location statistics showing the number of active and inactive actions a given location has. A simple JOIN like

SELECT count(nullif(actions.active, 1)) AS icount, 
       count(nullif(actions.active, 0)) AS acount, 
       things.name AS name, things.id AS thing_id, things.city_id AS city_id
  FROM "actions" 
  LEFT JOIN things ON actions.thing_id = things.id 
 WHERE UPPER(substring(things.name, 1, 1)) = UPPER('A') 
   AND actions.datetime_at BETWEEN '2012-09-26 19:52:14' AND '2012-10-26 22:00:00'
 GROUP BY things.name, things.id ORDER BY things.name

will give me a list of “things” (starting with ‘A’) which have actions associated with them and their active and inactive count like this:

icount | acount | name                      | thing_id | city_id
------------------------------------------------------------------
     0        5   Brooklyn, New York City   | 25       | 23
     1        0   Manhattan, New York City  | 24       | 23
     3        2   New York City             | 23       | 23

Now I would like to

  • only consider “city” things (that’s easy: filter by type in “things”), and
  • in the active/inactive counts, use the sum of all actions happening in this city – regardless of whether the action is associated with the city itself or something inside the city (= having the same city_id). With the same dataset as above, the new query should result in
icount | acount | name                      | thing_id | city_id
------------------------------------------------------------------
     4        7   New York City             | 23       | 23

I do not need the thing_id in this table (since it would not be unique anyway), but since I do need the city’s name (for display), it is probably just as easy to also output the ID, then I don’t have to change as much in my code.

How would I have to modify the above query to achieve this? I’d like to avoid additional trips to the database, and advanced SQL features such as procedures, triggers, views and temporary tables, if possible.

I’m using Postgres 8.3 with Ruby 1.9.3 on Rails 3.0.14 (on Mac OS X 10.7.4).

Thank you! 🙂

  • 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-06-11T22:56:50+00:00Added an answer on June 11, 2026 at 10:56 pm

    You need to count actions for all things in the city in an independent subquery and then join to a limited set of things:

    SELECT c.icount
          ,c.acount      
          ,t.name
          ,t.id AS thing_id
          ,t.city_id
    FROM  (
       SELECT t.city_id
             ,count(nullif(a.active, 1)) AS icount
             ,sum(a.active) AS acount
       FROM   things t 
       LEFT   JOIN actions a ON a.thing_id = t.id 
       WHERE  t.city_id = 23           -- to restrict results to one city
       GROUP  BY t.city_id
       ) c                             -- counts per city
    JOIN   things t USING (city_id)
    WHERE  t.name ILIKE 'A%'
    AND    t.datetime_at BETWEEN '2012-09-26 19:52:14'
                             AND '2012-10-26 22:00:00'
    ORDER  BY t.name, t.id;
    

    I also simplified a number of other things in your query and used table aliases to make it easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively large table (5,208,387 rows, 400mb data/670mb index), all columns i
I have this relatively large table in a separate filegroup (2 GB, well, it's
My relatively large program has a PDF help file which I have put together.
I have an image which has a relatively large area of transparency, alpha space.
I have a relatively large database (130.000+ rows) of weather data, which is accumulating
I have a relatively large database application that has a user table with a
I have a relatively large table (a couple millions of records), multiple websites are
We have a relatively large web application (>200 pages) to which we need to
I have a relatively large table (81M rows) and an index on it. I
The following two statements both have negligible execution times over a relatively large table.

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.