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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:46:22+00:00 2026-06-13T13:46:22+00:00

If I have a query like SELECT date_trunc(‘day’, assigndate)e, count(CASE WHEN a.assigneeid = 65548

  • 0

If I have a query like

SELECT date_trunc('day', assigndate)e,
       count(CASE WHEN a.assigneeid = 65548
             AND a.assigneeid IN
               (SELECT userid
                FROM groupmembers
                WHERE groupid = 65553) THEN 1 ELSE NULL END) assigned,
       count(CASE WHEN a.assigneeid = 65548
             AND a.completedtime IS NOT NULL
             AND a.assigneeid IN
               (SELECT userid
                FROM groupmembers
                WHERE groupid = 65553) THEN 1 ELSE NULL END) completed
FROM ASSIGNMENT a
WHERE assigndate > CURRENT_TIMESTAMP - interval '20 days'
GROUP BY date_trunc('day',assigndate);

The subquery in question is

SELECT userid
                FROM groupmembers
                WHERE groupid = 65553

then since the subquery is not co-related to the parent query, it will be executed just once and the cached result will be used. But since the subquery is present at 2 locations in the query, then according to the SQL plan, it is evaluated twice. Is there any way to cache the result of that subquery and use it at both the locations ?

The subquery can’t be converted to a join as is no single field on which to join (and it can’t be an unconditional join, as the count will become wrong then)

  • 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-13T13:46:23+00:00Added an answer on June 13, 2026 at 1:46 pm

    You can use a common table express (WITH)

    with cte as 
    (
         SELECT userid FROM groupmembers WHERE groupid = 65553
    )
    SELECT 
        date_trunc('day', assigndate)e,  
        count(CASE WHEN a.assigneeid = 65548 AND a.assigneeid IN  
               (SELECT userid from cte) then 1 else null end) assigned,
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales
Say, you have a query like SELECT COUNT(*), date FROM ORDERS GROUP BY date
I have the following query in ORACLE SQL: Select Trunc(Cs.Create_Dtime), Count(Case When Cs.Cs_Listing_Id Like
I have the following query: Select Count(Distinct Case When Play.Uuid Like ('i~%') Then Tap.Player_Id
I have query like this: SELECT DATE,REGION,COUNT(*) FROM ALL_ID_DATA WHERE DATE in (SUBDATE(CURDATE(),1),SUBDATE(CURDATE(),2),SUBDATE(CURDATE(),8)) AND
I have a query like this: select to_date(to_char(registime, 'YYYY-MM'),'YYYY-MM') as dt,count(id) as total_call from
I have a query like this select `temp`, IfNULL(count(id),0) t from survey where `temp`<>'NULL'
I have a query that looks like this SELECT * from myTable WHERE Date
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have a query like: SELECT ... FROM ... WHERE .. AND SomeID =

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.