Solved

Trying to create a computed column on a view

Posted on 2009-05-04
2
205 Views
Last Modified: 2012-05-06
I have a view I am trying to add a column to. The new column will be computed by taking 3 other columns. Essentially I want a sum of (ColA -(ColB+ColC)) . In my ALTER View statement I am trying to create a column  by using the statement below. Any idea as to how to do this.  I should mention i want totals to be per row and not a grand total. Any help would be greatly appreciated.
ALTER VIEW [dbo].[v_viewinvoicetot]

AS

SELECT SUM(orderTbl.totprod_nm - (orderTbl.sandh_nm + orderTbl.tax_d)) AS totprodnm_custom
 

FROM         invoiceTbl INNER JOIN

                      orderTbl ON invoiceTbl.orderid_i = orderTbl.orderid_i

Open in new window

0
Comment
Question by:jazzcatone
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 24298948
try this:

ALTER VIEW [dbo].[v_viewinvoicetot]
AS
SELECT *, orderTbl.totprod_nm - orderTbl.sandh_nm + orderTbl.tax_d AS totprodnm_custom
 
FROM         invoiceTbl INNER JOIN
                      orderTbl ON invoiceTbl.orderid_i = orderTbl.orderid_i
 


0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
ID: 24299636

Asker - do you want (ColA -(ColB+ColC)) or ColA-ColB+ColC ? chapmandew query will calculate ColA-ColB+ColC.
If you want ColA-(ColB+ColC), then try like this.
ALTER VIEW [dbo].[v_viewinvoicetot]

AS

SELECT *, orderTbl.totprod_nm - orderTbl.sandh_nm - orderTbl.tax_d AS totprodnm_custom

  FROM invoiceTbl 

 INNER JOIN orderTbl ON invoiceTbl.orderid_i = orderTbl.orderid_i

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What's the difference between these two "qualifiers?" 3 36
Calculating Business Hours 19 67
GRANT, REVOKE, DENY 4 20
Query 14 22
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

743 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

15 Experts available now in Live!

Get 1:1 Help Now