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 863803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:22:15+00:00 2026-05-15T09:22:15+00:00

Problem In the following query, plr_stations is called twice: once to limit the WHERE

  • 0

Problem

In the following query, plr_stations is called twice:

  1. once to limit the WHERE clause; and
  2. once to count the number of results it returned.

The code resembles:

  SELECT
 m.*,
 s.*,
 (
  SELECT
    count(1)
  FROM
    climate.plr_stations('48.5146','-123.4447')
 ) AS count_stations
  FROM 
 climate.station s,
 climate.measurement m,
 (
  SELECT
    id
  FROM
    climate.plr_stations('48.5146','-123.4447')
 ) stations
  WHERE
    s.applicable AND
    s.id = stations.id AND
    m.station_id = s.id AND ...

The results of this query are then aggregated by a date query.

Solutions

Populate a temporary table, or an array variable, with the results from the function call.

Update #1

The function call will randomly select a sample of stations if too many stations are inside the spherical polygon defined by the parameters to the function call.

Update #2

The date query aggregation that starts the full query looks as follows:

        SELECT 
          extract(YEAR FROM m.taken) AS year_taken,
          avg(m.amount) AS amount,
          count(m.amount) AS count_measurements,
          md.count_stations,
          min(md.elevation) AS elevation_min,
          max(md.elevation) AS elevation_max
        FROM
          climate.measurement m, (
          SELECT
            m.*,
            s.*, ...

Question

How else can the redundant call be eliminated?

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-05-15T09:22:16+00:00Added an answer on May 15, 2026 at 9:22 am

    Doubtful that it would fold that given that it shouldn’t be marked as IMMUTABLE (if I understand the intent as such).

    Something along this line should work…depending on your requirements…

    with R_stations as (
    SELECT
         id,   
         count(1) over () c
          FROM
            climate.plr_stations('48.5146','-123.4447')
    )
         SELECT
         m.*,
         s.*,
         stations.c count_stations
          FROM 
         climate.station s,
         climate.measurement m,
         R_stations stations
          WHERE
            s.applicable AND
            s.id = stations.id AND
            m.station_id = s.id AND ...
    

    But it may be easier considering your aggregation to just do this…

     SELECT
     m.*,
     s.*,
     stations.c count_stations
      FROM 
     climate.station s,
     climate.measurement m,
     (SELECT
     id,   
     count(1) over () c
      FROM
        climate.plr_stations('48.5146','-123.4447')
    
    ) stations
          WHERE
            s.applicable AND
            s.id = stations.id AND
            m.station_id = s.id AND ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem trying to get the count out of the following query:
I have problem with following query: SELECT g_contac.contid, g_contac.name, g_contac.email, f_sync.foreign_key, ( SELECT COUNT(g_cpers.cpersid)
Following problem: Query 2 and Query 3 depend on the results of Query 1
I am using following query which works fine for me except one problem SELECT
I have the following problem. I wrote a query to get the date of
I'm looking for help to create a query, to solve the following problem: Let's
In the following code, the header: line is giving problem. $q = mysql_query($a) or
so I'm having a problem with this following query.. So, the query is only
I have the following query: SELECT products_categories.categoryID, name, COUNT(*) AS itemCount FROM products_categories LEFT
Im using the following query to target results that are exactly X days older

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.