Calculating Columns in a View

  • Hi,

    Could some one tell me if it is possible to add an extra column to a view that contains a calculated value.

    I know how to do it to a table but need to know if it is possible to do in on a View.

    Many Thanks

  • yes you can, the syntax is the same as when you select something based on other data, just make sure the column name is assigned;

    ColumnName = [calculation,case statement, formula,etc]

    or

    [calculation,case statement, formula,etc] AS ColumnName

    examples:

    CREATE VIEW vw_example

    AS

    SELECT s.*,

    CASE

    WHEN s.status = 0

    THEN 'Open'

    WHEN s.status = 2

    THEN 'Closed'

    ELSE 'Pending'

    END AS CurrentStatus ,

    MyFormula = s.Paycheck - (s.Taxes + s.Insurance) + s.Bonus,

    s.Paycheck - (s.Taxes + s.Insurance) + s.Bonus As SameFormula

    FROM Example s

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply