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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:49:40+00:00 2026-06-15T05:49:40+00:00

I have a large but somewhat straightforward SQL query. Basically, users on my site

  • 0

I have a large but somewhat straightforward SQL query. Basically, users on my site develop reputations for different types of activities, such as writing reviews, leaving comments, and adding entries to our database. For the most part, these points are stored in the reputable_actions table, and I retrieve them by LEFT JOINing the reputable_actions table repeatedly. This feels sloppy, but it mostly work.

The problem I’m experiencing is with two of the reputations, “reviewer” and “community.” Unlike the others, they aren’t stored in the reputable_actions table. Instead, their values are derived from the votes table, which I access by first LEFT JOINing the comments table. For some reason, joining the comments table causes all my other reputations to increase exponentially. In one trial, the “archivist” reputation was suppose to be 25, but when I joined the comments, it ballooned to 10050.

I’m a novice with SQL and I’ve tried what I know (namely, applying GROUP BY clauses to users.id), but I haven’ had any luck yet. Some guidance would be greatly appreciated.

SELECT users.*,
  SUM(COALESCE(reviewers.value, 0)) as reviewer,
  SUM(COALESCE(communities.value,0)) as community,
  SUM(COALESCE(developers.value,0)) as developer,
  SUM(COALESCE(moderators.value,0)) as moderator,
  SUM(COALESCE(marketers.value,0)) as marketer,
  SUM(COALESCE(archivists.value,0)) as archivist,
  SUM(COALESCE(karmas.value,0)) as karma
FROM `users`
LEFT JOIN comments AS impressions
  ON impressions.user_id = users.id
  AND impressions.type = 'impression'
LEFT JOIN comments AS replies
  ON replies.user_id = users.id
  AND replies.type = 'reply'
LEFT JOIN votes AS reviewers
  ON reviewers.voteable_type = 'impression'
  AND reviewers.voteable_id = impressions.id
LEFT JOIN votes AS communities
  ON communities.voteable_type = 'reply'
  AND communities.voteable_id = replies.id
LEFT JOIN reputable_actions AS developers
  ON developers.reputation_type = 'developer'
  AND developers.user_id = users.id
LEFT JOIN reputable_actions AS moderators
  ON moderators.reputation_type = 'moderator'
  AND moderators.user_id = users.id
LEFT JOIN reputable_actions AS marketers
  ON marketers.reputation_type = 'marketer'
  AND marketers.user_id = users.id
LEFT JOIN reputable_actions AS archivists
  ON archivists.reputation_type = 'archivist'
  AND archivists.user_id = users.id
LEFT JOIN reputable_actions AS karmas
  ON karmas.reputation_type = 'karma'
  AND karmas.user_id = users.id
GROUP BY users.id
  • 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-15T05:49:41+00:00Added an answer on June 15, 2026 at 5:49 am

    Basically you need to do two separate group bys, and combine the results. There’s a trick to avoid joining multiple times, it may not by faster if you have many other voteable types, or reputation types.

    Select
      u.*,
      Coalesce(r.developer, 0) as developer,
      Coalesce(r.moderator, 0) as moderator,
      Coalesce(r.marketer, 0) as marketer,
      Coalesce(r.archivist, 0) as archivist,
      Coalesce(r.karma, 0) as karma,
      Coalesce(v.impressions, 0) as impressions,
      Coalesce(v.replies, 0) as replies
    From
      users u
        Left Outer Join (
        Select
          user_id,
          Sum(Case When reputation_type = 'developer' Then value Else 0 End) as developer,
          Sum(Case When reputation_type = 'moderator' Then value Else 0 End) as moderator,
          Sum(Case When reputation_type = 'marketer' Then value Else 0 End) as marketer,
          Sum(Case When reputation_type = 'archivist' Then value Else 0 End) as archivist,
          Sum(Case When reputation_type = 'karma' Then value Else 0 End) as karma
        From
          reputable_actions
        Group By
          user_id
      ) r On u.id = r.user_id
        Left Outer Join (
        Select
          c.user_id,
          Sum(Case When c.type = 'impression' Then v.value Else 0 End) as impressions,
          Sum(Case When c.type = 'reply' Then v.value Else 0 End) as replies
        From
          comments c
            inner join -- maybe left outer?
          votes v
            on v.voteable_type = c.type And v.voteable_id = c.id
        Group By
          user_id
      ) v On u.id = v.user_id
    

    Example (with no data). If your tables are structured differently to this, let me know.

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

Sidebar

Related Questions

I have a large project but I need only some folders in my svn
I have a large javascript which I didn't write but I need to use
how to animate a set of images, but I have a large set of
This may be a silly question, but right now I have a rather large
I have a calendar feed (.ics) available from my application, but it's quite large
I have a large collection of scanned images, and they are all somewhat skewed,
The task I have is to (somewhat efficiently) read line-by-line through a very large,
I have somewhat of a strange question that is not really technical, but I
I have a somewhat large output text file where I need to delete all
We have a large (somewhat unwieldy) project, that definitely has files, both Java classes

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.