Failed to create view in Microsoft SQL Server Management Studio

I am new to sql and struggling to pretend. This works and retrieves the data I need as a table, but when I try to do it as a view, I get an error:

SQL text cannot be represented in the grid area and chart pane.

SELECT [Data_Collector_ID],
       [Batch_Info_ID],
       [AssetTag],
       [DateTimeStamp],
       [Dust_Collector_DP]
FROM (
    SELECT [Data_Collector_ID],
           [Batch_Info_ID],
           [AssetTag],
           [DateTimeStamp],
           [Dust_Collector_DP],
           ROW_NUMBER() OVER (PARTITION BY [Batch_Info_ID] ORDER BY [DateTimeStamp] DESC) rn
    FROM [PLCLogging].[dbo].[Coater_Data_Collector]
) tmp
WHERE rn = 1 AND ([DateTimeStamp] > DATEADD(DAY, -7, GETDATE()))
    OR rn = 2 AND ([DateTimeStamp] > DATEADD(DAY, -7, GETDATE()))
ORDER BY DateTimeStamp DESC

Any idea how to make this work as a view?

I am trying to pull out the last two values ​​for each Batch_Info_IDin the last week. If I need to provide more details, please let me know.

+4
source share
1 answer

, .

:

CREATE VIEW MyViewName
AS
    SELECT [Data_Collector_ID],
           [Batch_Info_ID],
           [AssetTag],
           [DateTimeStamp],
           [Dust_Collector_DP]
    FROM (
        SELECT [Data_Collector_ID],
               [Batch_Info_ID],
               [AssetTag],
               [DateTimeStamp],
               [Dust_Collector_DP],
               ROW_NUMBER() OVER (PARTITION BY [Batch_Info_ID] ORDER BY [DateTimeStamp] DESC) rn
        FROM [PLCLogging].[dbo].[Coater_Data_Collector]
    ) tmp
    WHERE rn = 1 AND ([DateTimeStamp] > DATEADD(DAY, -7, GETDATE()))
        OR rn = 2 AND ([DateTimeStamp] > DATEADD(DAY, -7, GETDATE()))
    ORDER BY DateTimeStamp DESC
+4

Source: https://habr.com/ru/post/1623123/


All Articles