Solved

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

Posted on 2007-12-01
3
1,905 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Refresh Dev server with Production database 8 27
Linked Server Issue with SQL2012 3 23
Query Syntax 17 32
Can > be used for a Text field 6 35
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 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