|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:13 AM
Points: 20,
Visits: 71
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Today @ 2:10 AM
Points: 275,
Visits: 789
|
|
| When you say it's not working - do you mean it errors or it produces unexpected results?
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Today @ 2:10 AM
Points: 275,
Visits: 789
|
|
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]
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 4:51 AM
Points: 1,039,
Visits: 939
|
|
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]
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:13 AM
Points: 20,
Visits: 71
|
|
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!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:13 AM
Points: 20,
Visits: 71
|
|
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.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620,
Visits: 8,261
|
|
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
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 11:43 PM
Points: 4,553,
Visits: 8,207
|
|
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.
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 8,620,
Visits: 8,261
|
|
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
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 11:43 PM
Points: 4,553,
Visits: 8,207
|
|
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).
|
|
|
|