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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:16:49+00:00 2026-05-26T23:16:49+00:00

I have TableA in a many-to-many relationship with TableC via TableB. That is, TableA

  • 0

I have TableA in a many-to-many relationship with TableC via TableB. That is,

TableA       TableB           TableC
id | val     fkeyA | fkeyC    id | data

I wish the do select sum(val) on TableA, grouping by the relationship(s) to TableC. Every entry in TableA has at least one relationship with TableC. For example,

TableA
1 | 25
2 | 30
3 | 50

TableB
1 | 1
1 | 2
2 | 1
2 | 2
2 | 3
3 | 1
3 | 2

should output

75
30

since rows 1 and 3 in Table have the same relationships to TableC, but row 2 in TableA has a different relationship to TableC.

How can I write a SQL query for this?

  • 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-26T23:16:50+00:00Added an answer on May 26, 2026 at 11:16 pm
    SELECT    
       sum(tableA.val) as sumVal,    
       tableC.data  
    FROM    
       tableA 
         inner join tableB ON tableA.id = tableB.fkeyA 
         INNER JOIN tableC ON tableB.fkeyC = tableC.id  
    GROUP by tableC.data
    

    edit
    Ah ha – I now see what you’re getting at. Let me try again:

    SELECT
       sum(val) as sumVal,
       tableCGroup
    FROM
    (
    
    SELECT 
       tableA.val,
       (
          SELECT cast(tableB.fkeyC as varchar) + ',' 
          FROM tableB WHERE tableB.fKeyA = tableA.id
          ORDER BY tableB.fkeyC
          FOR XML PATH('') 
       ) as tableCGroup
    FROM
       tableA
    
    
    ) tmp
    GROUP BY
        tableCGroup
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, that relate via a one-to-many relationship i.e tableOne (1)----------(*) tableTwo
i have a many to many table relationship that involves 2 logical tables. Record
I have the following tables in my database that have a many-to-many relationship, which
I have two tables that are connected via a join table in a many-to-many
I have tables Users, Widgets and Layouts. Users have many-to-many relationship with Widgets via
We have two tables, ActivityForm and Field which are given a many-to-many relationship via
Using CoreData, I have an entity Bookmark, that has an to-many relationship named 'tags'
I have two tables books and tags which are in a many-to-many relationship via
I have an existing many-to-many relationship in SQL that is being mapped to my
I have a one to many relationship between two tables. The many table contains

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.