computed column in view

  • I have the following "create view" query that s not working, can't figure out what's wrong... :

    use test1

    go

    create view dbo.GlobaldataEOLT2

    as

    select[NUMCAM]

    ,[C_Fourn]

    ,[date_passage]

    ,[heure_passage]

    ,[banc]

    ,[NUMPale]

    ,[Module_Status]

    ,[Code_defaut]

    ,[Total_TestTime]

    ,[Purgerunin_TestTime]

    ,[ShifterPosHyst_TestTime]

    ,[ShifterActRespTime_TestTime]

    ,[GearActStroke_TestTime]

    ,[GearActRespTime_TestTime]

    ,[LeakageON_TestTime]

    ,[K1K2Leakage_TestTime]

    ,[QPVK1DeliveryTest_TestTime]

    ,[PPVK2PressureTest_TestTime]

    ,[LeakageOFF_TestTime]

    ,[PRVTest_TestTime]

    ,[OPS_NRV_Test_TestTime]

    ,[version_prog]

    ,[Jourprod] AS

    CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2

    ELSE day([date_passage]) -1

    END

    FROM dbo.T_EOLT_BRUTE_FLOAT

  • When you say it's not working - do you mean it errors or it produces unexpected results?

  • How about = instead of 'AS'

    create view dbo.GlobaldataEOLT2

    as

    select [NUMCAM]

    ,[C_Fourn]

    ,[date_passage]

    ,[heure_passage]

    ,[banc]

    ,[NUMPale]

    ,[Module_Status]

    ,[Code_defaut]

    ,[Total_TestTime]

    ,[Purgerunin_TestTime]

    ,[ShifterPosHyst_TestTime]

    ,[ShifterActRespTime_TestTime]

    ,[GearActStroke_TestTime]

    ,[GearActRespTime_TestTime]

    ,[LeakageON_TestTime]

    ,[K1K2Leakage_TestTime]

    ,[QPVK1DeliveryTest_TestTime]

    ,[PPVK2PressureTest_TestTime]

    ,[LeakageOFF_TestTime]

    ,[PRVTest_TestTime]

    ,[OPS_NRV_Test_TestTime]

    ,[version_prog]

    ,[Jourprod] =

    CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2

    ELSE day([date_passage]) -1

    END

    FROM dbo.T_EOLT_BRUTE_FLOAT

    or

    CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2

    ELSE day([date_passage]) -1

    END as [Jourprod]

  • Could you please recheck the CASE statement...I think it should be..

    -- [Jourprod] AS

    CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2

    ELSE day([date_passage]) -1

    END AS [Jourprod]

  • Indeed!

    It works this way:

    select

    ,....

    ,[version_prog]

    ,(CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN (day(date_passage) -2)

    ELSE (day([date_passage]) -1)

    END) AS jourprod

    FROM dbo.T_EOLT_BRUTE_FLOAT

    Thanks Laurie, Asiaindian!

  • Another problem linked to this one..

    The query works fine now, even if i don't like not being able to write "jprod as....query treatement here"

    What if i need to define the computed column "jprod" as persisted?

    I tried to put the keyword PERSISTED wherever it could be and i got an error in each case.

    SELECT [NUMCAM]

    ,[date_passage]

    ,[heure_passage]

    ,[anneeprod]

    ,(CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(date_passage) - dbo.jprodtestannee(anneeprod)

    ELSE day(date_passage) - dbo.jprodtestannee(anneeprod) +1

    END) as jprod

    FROM dbo.GlobaldataEOLT8

    GO

    I tried to re-write the query the way i feel more comfortable with :

    ..jprod as (CASE WHEN....) PERSISTED but the error remains, can't get it solved.

  • ohpenot (2/25/2013)


    Another problem linked to this one..

    The query works fine now, even if i don't like not being able to write "jprod as....query treatement here"

    What if i need to define the computed column "jprod" as persisted?

    I tried to put the keyword PERSISTED wherever it could be and i got an error in each case.

    SELECT [NUMCAM]

    ,[date_passage]

    ,[heure_passage]

    ,[anneeprod]

    ,(CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(date_passage) - dbo.jprodtestannee(anneeprod)

    ELSE day(date_passage) - dbo.jprodtestannee(anneeprod) +1

    END) as jprod

    FROM dbo.GlobaldataEOLT8

    GO

    I tried to re-write the query the way i feel more comfortable with :

    ..jprod as (CASE WHEN....) PERSISTED but the error remains, can't get it solved.

    PERSISTED is used for computed columns. You are creating a view, this means the calculation will have to run everytime you select the data. If you want to avoid this you would need to add the computed column to your base table.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (2/25/2013)


    .. you would need to add the computed column to your base table.

    or create an indexed view.

    There are requirements to meet though - check BOL for details.

    _____________
    Code for TallyGenerator

  • Sergiy (2/25/2013)


    Sean Lange (2/25/2013)


    .. you would need to add the computed column to your base table.

    or create an indexed view.

    There are requirements to meet though - check BOL for details.

    Not sure I follow you here. You don't need to have an indexed view to have a calculation in it. The point of using PERSISTED is so that the engine does not have to calculate the value every time you retrieve a row. It does this by physically storing the results in the table. A view does not store any data so the calculation would not be stored. I agree though that an indexed view sounds like the best solution for the OP's situation.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (2/27/2013)


    . A view does not store any data so the calculation would not be stored.

    An indexed view does.

    Another name for it is "materialized view" - because all the calculations and conversions are actually stored in the index(es).

    _____________
    Code for TallyGenerator

  • Thank you very much for all your answers.

    That's what i need.I am going to work with indexed views. Except that i finally decided to add certain computed columns directly to base tables, so that i can focus on aggregate functions in views.

Viewing 11 posts - 1 through 10 (of 10 total)

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