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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:02:07+00:00 2026-06-15T07:02:07+00:00

here is the sqlonline There is a band. Each member plays an instrument. each

  • 0

here is the sqlonline

There is a band.

Each member plays an instrument.

each instrument costs X$

I would like to create a report : how much each member ‘costs’ to the band manager.

create table #Inst(id int, name nvarchar(max) , price int )
insert into  #Inst values  (1,'guitar',20), 
                           (2,'bass',10),
                           (3,'drums',30),
                           (4,'piano',40) 
  
  create table #Players(id int, name nvarchar(max) , instId int )
  insert into  #Players values (1,'john',1), 
                               (2,'john',4),
                               (3,'paul',2),
                               (4,'paul',2),
                               (5,'george',1),
                               (6,'ringo',3)

What have I tried ?

select name , totalCostToTheband=   
              (select sum(price) from #inst i where i.id= #players.instId ) 
from #players

but this yields duplicate names :

enter image description here

I want to see 4 records which summarize each name’s costs.

p.s. :
I could have created a sum() over the whole query , but i want to know if there is a better , elegant solution.

  • 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-15T07:02:09+00:00Added an answer on June 15, 2026 at 7:02 am

    In the current schema you should do

    SELECT 
        p.name , 
        sum(price) as totalCostToTheband
    FROM
        #players p 
        INNER JOIN #inst i ON i.id = p.instId
    GROUP BY
        p.name
    

    But when you have many-to-many relations you normally do a middle table with the links

    create table #Inst(id int, name nvarchar(max) , price int )
    create table #Players(id int, name nvarchar(max) )
    create table #instplayer(id int, inst_id, player_id )
    

    This saves from duplicating the players inside the #players table, and also allows for players with the same name to be in the same band 😉

    Read http://sqlrelationship.com/many-to-many-relationship/ for more info

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

Sidebar

Related Questions

Here is a generic class that I have defined, what I would like to
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here's a coding problem for those that like this kind of thing. Let's see
Here's what my table TheTable looks like ColA | ColB | ColC ------+-------+------ abc
Here's my procedure: DROP PROCEDURE IF EXISTS `couponExpires`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `couponExpires`(IN couponID BIGINT,
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is my problem. Our tech. writer or customer service will often create new
Here is my problem.. I have the option to create(add) two new input fields,

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.