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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:45:59+00:00 2026-05-17T16:45:59+00:00

Continuing on my last question … Let me try to explain my schema. I

  • 0

Continuing on my last question…

Let me try to explain my schema. I have three tables we’ll call users (with columns id and name), parties (with columns id, partydate, and user_id) and questions (with columns id, createdate, and user_id). My requirement is to show for every user the number of parties within the last year and questions created within the last year.

My query looks like:

SELECT users.id, users.name,  
  SUM(CASE WHEN (parties.party> NOW() - interval '1 year') THEN 1 ELSE 0 END) 
    AS numparties, 
  SUM(CASE WHEN (questions.createdate> NOW() - interval '1 year') THEN 1 ELSE 0 END)
    AS numquestions
FROM users
  LEFT JOIN parties ON users.id=parties.user_id
  LEFT JOIN questions ON users.id=questions.user_id
GROUP BY users.id, users.name;

This works almost 100%. I am getting a result with all users that exist. The problem is, for some users (a very small few) I’m counting either a party or a question twice. For example, if I change the above query to just show parties.id and questions.id instead of summing them as well as remove the GROUP BY, I might get something like:

user.id | user.name | parties.id | questions.id  
-----------------------------------------------
0          John          15             2
0          John          15             7

You can see it shows the parties.id twice.

When I was using COUNT() I could rely on DISTINCT but with SUM I’m not sure how I can. I want something like:

SUM(CASE WHEN (parties.party> NOW() - interval '1 year' AND parties.id IS DISTINCT) THEN 1 ELSE 0 END) 
AS numparties, 

But of course this isn’t valid. Can this small problem be corrected easily?

  • 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-17T16:46:00+00:00Added an answer on May 17, 2026 at 4:46 pm

    I won’t write the code for you (since it’s homework), but you’ll want to put the two calculations into subqueries.

    Here’s a template:

      SELECT users.id, users.name, 
             subquery1.result_of_calculation1, subquery2.result_of_calculation2
        FROM users
             LEFT JOIN (
                --calculation 1
             ) subquery1
             ON users.id = subquery1.user_id
             LEFT JOIN (
                --calculation 2
             ) subquery2
             ON users.id = subquery2.user_id
    GROUP BY users.id, users.name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing from my previous question , is there a comprehensive document that lists all
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I have two tables containing Tasks and Notes, and want to retrieve a list
Continuing my investigation of expressing F# ideas in C#, I wanted a pipe forward
Due to continuing crash problems, I'm about to uninstall and reinstall my copy of
I have a column containing the strings 'Operator (1)' and so on until 'Operator
I have a dl containing some input boxes that I clone with a bit
I have a table containing prices for a lot of different things in a
I have a string containing a date, and another string containing the date format
Given a table (mytable) containing a numeric field (mynum), how would one go about

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.