Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when creating a view with a CTE

Think I am being stupid but am getting the error:

Msg 195, Level 15, State 1, Procedure VW_THIRDPARTY_SLA_REPORT_MONTHLY_GP_NONAGGREGATE, Line 8 'partitioned' is not a recognized option.

when trying to execute the following create view statement

CREATE VIEW [dbo].[VW_THIRDPARTY_SLA_REPORT_MONTHLY_GP_NONAGGREGATE] 
With partitioned
AS 
(Select 
B.MSH7_DateTimeOfMessage,
B.PID2x1_PatientIDExternal,
B.PID3x1_PatientIDInternal,
B.PID5x1_PatientName_FamilyName,
B.PV3x2_AssignedPatientLocation_Room,
A.OBR4x2_UniversalServiceID_Text,
A.OBX3x2_ObservationIdentifier_Text,
A.OBR24_DiagnosticServiceSectionID,
A.OBR6_RequestDateTime,
C.TestName,
C.PriceBaseline,
D.Contract,
Row_NUMBER()

OVER(Partition By [ORC3_FillerOrderNumber], [OBX3x2_ObservationIdentifier_Text] order by [ORC9_DateTimeOfTransaction]) as seq
From [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_Detail] A
LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_Header] B ON A.[DETAIL_ID] = B.[HEADER_ID]
LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_View_TFCData] C ON A.[OBR24_DiagnosticServiceSectionID] + A.[OBX3x1_ObservationIdentifier_Identifier] = C.[KEY]
LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_LocationDetail] D ON B.[PV3x1_AssignedPatientLocation_PointOfCare] = D.[PracticeCode] 

)
Select *
from partitioned  
where seq =1 

This is a query that is working nicely in a stored procedure so I am happy with the query just can't create it as a view.

Any help would be much appreciated

like image 485
haPartnerships Avatar asked Feb 13 '13 11:02

haPartnerships


People also ask

Can you use a CTE to create a view?

A Common Table Expression, also called as CTE in short form, is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. The CTE can also be used in a View.

Can we have 2 CTE in view?

After learning common table expressions or CTEs, a natural question is “Can I use several CTEs in one query?” Yes, you can!

What is not allowed in CTE query definition?

Specifying more than one WITH clause in a CTE isn't allowed. For example, if a CTE query definition contains a subquery, that subquery can't contain a nested WITH clause that defines another CTE. An ORDER BY clause can't be used in the CTE_query_definition, except when a TOP clause is specified.

What are the disadvantages of using CTE in SQL Server?

Disadvantages of CTE CTE's members cannot use the following clauses of keywords Distinct, Group By, Having, Top, Joins limiting by this type of the queries that can be created and reducing their complexity. The Recursive member can refer to the CTE only once.


1 Answers

You are missing the first AS after the CREATE VIEW:

CREATE VIEW [dbo].[VW_THIRDPARTY_SLA_REPORT_MONTHLY_GP_NONAGGREGATE] 
AS --- this is missing
  With partitioned
  AS 
  (
   Select 
      B.MSH7_DateTimeOfMessage,
      B.PID2x1_PatientIDExternal,
      B.PID3x1_PatientIDInternal,
      B.PID5x1_PatientName_FamilyName,
      B.PV3x2_AssignedPatientLocation_Room,
      A.OBR4x2_UniversalServiceID_Text,
      A.OBX3x2_ObservationIdentifier_Text,
      A.OBR24_DiagnosticServiceSectionID,
      A.OBR6_RequestDateTime,
      C.TestName,
      C.PriceBaseline,
      D.Contract,
      Row_NUMBER() OVER(Partition By [ORC3_FillerOrderNumber], [OBX3x2_ObservationIdentifier_Text] order by [ORC9_DateTimeOfTransaction]) as seq
   From [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_Detail] A
   LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_Header] B ON A.[DETAIL_ID] = B.[HEADER_ID]
   LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_View_TFCData] C ON A.[OBR24_DiagnosticServiceSectionID] + A.[OBX3x1_ObservationIdentifier_Identifier] = C.[KEY]
   LEFT OUTER JOIN [NWLHPathApp_DataWarehouse].[dbo].[PathologyHL7_LocationDetail] D ON B.[PV3x1_AssignedPatientLocation_PointOfCare] = D.[PracticeCode] 

   )
   Select *
   from partitioned  
   where seq =1 
like image 181
Taryn Avatar answered Oct 05 '22 02:10

Taryn