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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:59:20+00:00 2026-05-25T11:59:20+00:00

SELECT SUM(case when p.status = 2 then p.value end) as ‘val_accepted’ FROM props AS

  • 0
SELECT SUM(case when p.status = 2 then p.value end) as 'val_accepted'
      FROM
        props AS p
            INNER JOIN (p_contents AS pc
              INNER JOIN contents AS c ON c.id = pc.library_id)
            ON p.id = pc.prop_id
      WHERE p.account_id = 3
      GROUP BY (pc.library_id)

so, what’s happening:
there are two p_contents that are associated with a prop. those two p_contents have the same library_id which points to a corresponding content.
So, the SUM of p.value is double what it should be because there are two p_contents that point to the same content

How do I not double SUM the p.value?

EDIT:
I figured out how to use DISTINCT, but I still need access to the inner columns…

SELECT    c.name as 'library_name',
   SUM(case when p.status = 2 then p.value end) as 'val_accepted',

FROM
  props AS p
  INNER JOIN 
  (
    SELECT DISTINCT(pc.library_id), prop_id
    FROM prop_contents AS pc
    INNER JOIN 
    (
      SELECT name, visibility, id, updated_at 
      FROM contents AS c
    ) as c
      ON c.id = pc.library_id
  )as pc
  ON p.id = pc.prop_id
 WHERE p.account_id = 3
 GROUP BY (pc.library_id)

and now I get the error:

Unknown column 'c.name' in 'field list')
  • 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-25T11:59:20+00:00Added an answer on May 25, 2026 at 11:59 am

    Here’s one solution. First reduce the set to distinct rows in an derived table, then apply the GROUP BY to that result:

    SELECT SUM(case when d.status = 2 then d.value end) as 'val_accepted'
    FROM (
      SELECT DISTINCT p.id, p.status, p.value, pc.library_id
      FROM props p
      INNER JOIN p_contents AS pc ON p.id = pc.prop_id
      INNER JOIN contents AS c ON c.id = pc.library_id
      WHERE p.account_id = 3) AS d
    GROUP BY d.library_id
    

    You use DISTINCT(pc.library_id) in your example, as if DISTINCT applies only to the column inside the parentheses. This is a common misconception. DISTINCT applies to all columns of the select-list. DISTINCT is not a function; it’s a query modifier.

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

Sidebar

Related Questions

SELECT SUM( CASE WHEN cumulative = 1 THEN percent ELSE 0 END) FROM phppos_items_taxes;
Here's my query so far: SELECT posts.title , SUM(CASE comments.status WHEN 'approved' THEN 1
I can write Select Sum(Case When Resposta.Tecla = 1 Then 1 Else 0 End)
here's a regular SQL statement: SELECT SUM(CASE WHEN [Column2] = 'Cylinder' THEN 1 ELSE
================= This is my query: SELECT SUM(sub_total) AS sales, CASE WHEN (sub_total<100) THEN '0-99'
When I do this: select col1,case when [pivot1]=1 then '-' else '' end [pivot1],
SELECT user_id, SUM(COALESCE(point_points, 0)) AS total_points, SUM( CASE WHEN point_date > '$this_month' THEN point_points
Let's say I have a select Statement with the following: SUM(st.tafPoints/200) as totalFriendReferrals, SUM(CASE
SELECT sum(TotalHoursM) + (TotalHoursT) + (TotalHoursW) + (TotalHoursTH) + (TotalHoursF) AS TOTAL FROM LeaveRequest
SELECT SUM(a.Clicks) AS Clicks, SUM(b.NoOfUsers) Users, c.WEEK_NUM, b2.ALL_TASKS FROM (SELECT SUM(CLICK_CNT) AS Clicks, TO_CHAR(RQST_DT,'YYYY-MM-DD')

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.