Solved

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

Posted on 2007-12-01
3
1,932 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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