Самостоятельное присоединение, чтобы получить первый classID для отчета - PullRequest
0 голосов
/ 01 июля 2019

У меня есть следующая таблица:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[myTable]
(
    [importID] [INT] IDENTITY(1,1) NOT NULL,
    [classID] [INT] NOT NULL,
    [priorReportID] [INT] NOT NULL,
    [currentReportID] [INT] NOT NULL,
    [dateStamp] AS (GETDATE())
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[myTable] ON 
GO

INSERT INTO [dbo].[myTable] ([importID], [classID], [priorReportID], [currentReportID]) 
VALUES (1, 2069, 3825, 3833), (2, 2069, 3826, 3834),
       (3, 2069, 3827, 3835), (4, 2069, 3832, 3836),
       (5, 2091, 3889, 3890), (6, 2095, 3894, 3895),
       (7, 2098, 3895, 3898), (8, 2098, 3896, 3899),
       (9, 2098, 3897, 3900), (10, 2097, 2190, 2193),
       (11, 2096, 2188, 2190), (12, 2094, 2187, 2188),
       (13, 2093, 2180, 2187)
GO

SET IDENTITY_INSERT [dbo].[myTable] OFF
GO

Я пытаюсь получить первый ClassID при создании определенного отчета.

select * 
from mytable

select * 
from mytable
where currentReportID = 3833

select * 
from mytable
where currentReportID = 3825

select * 
from mytable
where currentReportID = 2193

select * 
from mytable
where currentReportID = 2190

select * 
from mytable
where currentReportID = 2188

select * 
from mytable
where currentReportID = 2187

select * 
from mytable
where currentReportID = 2180

В приведенном выше примере: reportID = 2193 был фактически создан в classID = 2093.

Аналогично, reportID = 3833 был создан в classID = 2069.

В основномМне нужно просмотреть записи до тех пор, пока не будет выполнено условие currentReportID = priorReportID.

Заранее спасибо.

...