Solved

Set Computed Column Specification Or field in Select as sum of child records

Posted on 2007-12-01
3
1,882 Views
Last Modified: 2010-04-21
I want to set a column in my invoice table to be a sum of the values in my invoice items table using computed column specification.

Example: 3 invoice items with a value of 10 in the total field would make the total field in the invoice table = 30 where the invoice.invoiceid value = the invoice items. invoiceid

If that's not possible how would I do it in my select from the invoice table so that my select returns one row for each invoice but has a column that has a sum of all related invoiceitem.total fields for that invoiceid? Something like:
Select SUM(invoiceitem.total) as total, invoiceid from invoices
0
Comment
Question by:advlgx
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20388390
>>I want to set a column in my invoice table to be a sum of the values in my invoice items table using computed column specification. <<
No that is not possible.  You could do it with a Select statement and a Group clause.  But without knowing the table structure it is difficult to be precise.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 20388396
Here is another option using a subquery:

Select Col1, Col2, Col3,
           (Select SUM(Total) From invoiceitem Where invoiceid = i.invoiceid)
From   invoice i
0
 

Author Closing Comment

by:advlgx
ID: 31412099
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now