Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

computed column in view Expand / Collapse
Author
Message
Posted Thursday, February 21, 2013 8:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 31, 2013 7:28 AM
Points: 20, Visits: 106
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
Post #1422624
Posted Thursday, February 21, 2013 8:33 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
When you say it's not working - do you mean it errors or it produces unexpected results?
Post #1422629
Posted Thursday, February 21, 2013 8:35 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
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]


Post #1422631
Posted Thursday, February 21, 2013 8:36 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 9:15 AM
Points: 1,271, Visits: 1,065

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]

Post #1422633
Posted Thursday, February 21, 2013 8:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 31, 2013 7:28 AM
Points: 20, Visits: 106
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!
Post #1422636
Posted Monday, February 25, 2013 8:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 31, 2013 7:28 AM
Points: 20, Visits: 106
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.
Post #1423645
Posted Monday, February 25, 2013 9:15 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1423659
Posted Monday, February 25, 2013 8:29 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
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.
Post #1423863
Posted Wednesday, February 27, 2013 7:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:36 PM
Points: 13,111, Visits: 11,946
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1424546
Posted Wednesday, February 27, 2013 9:31 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:39 PM
Points: 4,576, Visits: 8,342
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).
Post #1424881
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse