SQL Server 2012 add value of rows with matching ids

Kevin

I'm doing an assignment on SQL Server 2012 where I have to

7) Create a stored procedure (call it SQL7) which will retrieve the Charity ID and Charity Name and the total of all the contribution amounts that each charity has in the contribution table."

Charity ID and Charity Name are in one table and the second table has CharityID and Total contributions.

I don't know how to add the total contributions that each charity has received and output it. The code I have so far is

create proc SQL7
as
   select distinct 
       dbo.CharityTbl.CharityID, CharityName,
   from 
       dbo.CharityTbl, dbo.ContributionsTbl
   where 
      dbo.CharityTbl.CharityID = dbo.ContributionsTbl.CharityID

Thanks in advance!

zedfoxus

You'll probably need grouping (assuming table1 holds charity information and table2 holds contribution information)

create procedure SQL7
as
  select a.charityid, a.charityname, sum(b.totalcontributions) as totals
  from CharityTbl a
  left join ContributionsTbl b on a.charityid = b.charityid
  group by a.charityid, a.charityname

SQLFiddle example: http://sqlfiddle.com/#!3/67cca and http://sqlfiddle.com/#!3/ca00e

create table charityTbl (charityid int, charityname varchar(100));
insert into charityTbl values (1, 'Red Cross'), (2, 'Doctors without borders');

create table contributionsTbl (charityid int, totalcontributions int);
insert into contributionsTbl values
(1, 100),
(1, 200),
(2, 500);

(Just think that this was a stored procedure and was called with exec SQL7)

 select a.charityid, a.charityname, sum(b.totalcontributions) as totals
  from CharityTbl a
  left join ContributionsTbl b on a.charityid = b.charityid
  group by a.charityid, a.charityname

Result:

| charityid |             charityname | totals |
|-----------|-------------------------|--------|
|         2 | Doctors without borders |    500 |
|         1 |               Red Cross |    300 |

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

In SQL Server, how do I select IDs where value is in list, then insert matching IDs into join table?

分類Dev

SQL Server 2012のJSON_VALUE?

分類Dev

how to fetch matching names to ids on an sql table?

分類Dev

Add extra rows in result set of query with dummy data in sql server

分類Dev

Sum rows value if their IDs are in a defined range

分類Dev

Numbering islands in SQL Server 2012

分類Dev

SQL Server 2012 Pivot Table

分類Dev

Restore SQL Server 2012 error

分類Dev

SQL Server - Rows to column based on third column value

分類Dev

Count number of IDs in one column matching multiple defined IDs (rows) in another column

分類Dev

ASP.Net cannot add byte value to my SQL Server

分類Dev

How to update table and add column value in SQL Server?

分類Dev

SQL Server Command Builder and AutoIncrementing IDs

分類Dev

SQL Server : increase the value by one in a field where matching foreign key id

分類Dev

No process is on the other end of the pipe (SQL Server 2012)

分類Dev

SQL Server2012のRANDBETWEEN

分類Dev

Requires SQL Server 2012 Express LocalDB

分類Dev

How to check history in SQL Server 2012?

分類Dev

Move database from SQL Server 2012 to 2008

分類Dev

SQL Server 2012 Pivot Dynamic with Values?

分類Dev

SQL Server2014とSQLServer 2012

分類Dev

Merge data into one column - sql server 2012

分類Dev

authentication with node.js in SQL server 2012

分類Dev

PHP Connection to MS SQL Server 2012

分類Dev

MS-SQl Server 2012 for ubuntu

分類Dev

SQL Server2012で転置

分類Dev

SQL Server2012クエリ

分類Dev

sql server creates unneeded rows

分類Dev

Pandas how to add the counters for matching rows between two dataframe columns

Related 関連記事

ホットタグ

アーカイブ