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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:28:06+00:00 2026-05-19T05:28:06+00:00

For simplicity, I will give a quick example of what i am trying to

  • 0

For simplicity, I will give a quick example of what i am trying to achieve:

Table 1 – Members

  ID    |   Name
--------------------
  1     |   John    
  2     |   Mike    
  3     |   Sam  

Table 1 – Member_Selections

  ID    |   planID
--------------------
  1     |   1    
  1     |   2    
  1     |   1    
  2     |   2    
  2     |   3    
  3     |   2    
  3     |   1    

Table 3 – Selection_Details

planID  |   Cost
--------------------
  1     |   5    
  2     |   10    
  3     |   12  

When i run my query, I want to return the sum of the all member selections grouped by member. The issue I face however (e.g. table 2 data) is that some members may have duplicate information within the system by mistake. While we do our best to filter this data up front, sometimes it slips through the cracks so when I make the necessary calls to the system to pull information, I also want to filter this data.

the results SHOULD show:

Results Table

ID  |    Name    | Total_Cost
-----------------------------
1   |    John    |   15
2   |    Mike    |   22
3   |    Sam     |   15

but instead have John as $20 because he has plan ID #1 inserted twice by mistake.

My query is currently:

SELECT
    sq.ID, sq.name, SUM(sq.premium) AS total_cost
FROM
(
    SELECT
    m.id, m.name, g.premium
    FROM members m
    INNER JOIN member_selections s USING(ID)
    INNER JOIN selection_details g USING(planid)
) sq group by sq.agent

Adding DISTINCT s.planID filters the results incorrectly as it will only show a single PlanID 1 sold (even though members 1 and 3 bought it).

Any help is appreciated.

EDIT

There is also another table I forgot to mention which is the agent table (the agent who sold the plans to members).

the final group by statement groups ALL items sold by the agent ID (which turns the final results into a single row).

  • 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-19T05:28:08+00:00Added an answer on May 19, 2026 at 5:28 am

    Perhaps the simplest solution is to put a unique composite key on the member_selections table:

     alter table member_selections add unique key ms_key (ID, planID);
    

    which would prevent any records from being added where the unique combo of ID/planID already exist elsewhere in the table. That’d allow only a single (1,1)

    comment followup:

    just saw your comment about the ‘alter ignore…’. That’s work fine, but you’d still be left with the bad duplicates in the table. I’d suggest doing the unique key, then manually cleaning up the table. The query I put in the comments should find all the duplicates for you, which you can then weed out by hand. once the table’s clean, there’ll be no need for the duplicate-handling version of the query.

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

Sidebar

Related Questions

I'm using Java 6. For simplicity, everything will be public. Suppose I have this
For simplicity, I generally split a lot of my configuration (i.e. the contents of
For simplicity lets say I have two flex mxml pages. form.mxml button.mxml If the
For simplicity, i have the following file named test.jsp: <script language=javascript> alert(a$b.replace(/\$/g,k)); </script> I
For the sake of simplicity: There is a permission based system in place with
Let me apologize in advance for the simplicity of this question (I heard Jeff's
I'm impressed with the simplicity of Microsoft's Virtual Earth Street Address search service. My
One of the really nice things about python is the simplicity with which you
I am already excited about document databases and especially about CouchDB's simplicity. But I
Are there instances where switch(case) is is a good design choice (except for simplicity)

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.