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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:09:44+00:00 2026-06-04T20:09:44+00:00

SELECT CASE WHEN avg(count)>12 THEN 5 WHEN avg(count)>8 THEN 4 WHEN avg(count)>2 THEN 3

  • 0
SELECT CASE WHEN avg(count)>12 THEN 5
            WHEN avg(count)>8  THEN 4
            WHEN avg(count)>2  THEN 3
            WHEN avg(count)>1  THEN 2
            ELSE 1
            END,madeby
FROM (SELECT M.month,(SELECT count(*)
                     FROM Booking
                     WHERE date_trunc('month',starttime)=month
                         AND madeby=M.madeby
                     ) AS count,M.madeby
     FROM (SELECT date_trunc('month',generate_series(min(starttime),
                            current_timestamp,interval '1 month')::timestamp)
                            AS month,madeby
          FROM Booking
          GROUP BY madeby
     ) AS M
) AS BookingsPerMonth
GROUP BY madeby;

Now I need to do that computation there, I’m not questioning that. I’m guessing the problem is that it’s computing the aggregate function avg four times. The difference in times is from ~17 seconds with the CASE to ~4.5 seconds without (although I’m then lacking sufficient information)

Is there a way I can optimise this, through variable storage or something, such to bring the time closer to 4.5 seconds?

  • 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-04T20:09:46+00:00Added an answer on June 4, 2026 at 8:09 pm
    SELECT  
        CASE 
            WHEN avg_count > 12 THEN 5
            WHEN avg_count > 8  THEN 4
            WHEN avg_count > 2  THEN 3
            WHEN avg_count > 1  THEN 2
            ELSE 1
        END,
        madeby
    FROM (
        SELECT avg(count) as avg_count, madeby
        FROM (
            SELECT 
                M.month,
                (
                    SELECT count(*)
                    FROM Booking
                    WHERE date_trunc('month',starttime)=month AND madeby=M.madeby
                ) AS count, 
                M.madeby
            FROM (
                SELECT 
                    date_trunc('month',generate_series(min(starttime),
                    current_timestamp,interval '1 month')::timestamp) AS month,
                    madeby
                FROM Booking
                GROUP BY madeby
            ) AS M
        ) AS BookingsPerMonth
        GROUP BY madeby
    ) AS Averages;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

select case when cntrctr_lcns_seq_no is null then 1 else max(cntrctr_lcns_seq_no) end as cntrctr_lcns_seq_no from
SELECT CASE Price WHEN '' THEN 0.0 ELSE Price END FROM Table If price
select CASE WHEN ( rtt.us_code = 's' ) THEN rtt.name ELSE '' END AS
select case when ''!='abcd' then 'nodate' else 'date' end; Here I hope '' stand
SELECT CASE Forms!FormName!ComboBox WHEN Is Not Null THEN (ParentTable.Column) FROM (Parent) WHERE (((ParentTable.Column)=Forms!FormName!ComboBox)) ELSE
SELECT ((CASE WHEN (qid2.AgeBelow_16 - qid1.AgeBelow_16)= 0 THEN 1 ELSE (qid2.AgeBelow_16- qid1.AgeBelow_16) END )/
This works: SELECT (CASE WHEN x = 'value' THEN a.col1 ELSE nvl(a.col1, a.col2) END)
my SQL query which works fine is select case month(timestamp_iso(STATUSDATE)) when 1 then 'January'
I have this SQL query select case when AllowanceId is null then 2 else
How to achieve the following query - SELECT CASE WHEN COUNT(*) > 0 THEN

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.