Добро пожаловать в форум, Guest  >>   Войти | Регистрация | Поиск | Правила | В избранное | Подписаться
Все форумы / Microsoft SQL Server Новый топик    Ответить
 Помощь с запросом  [new]
romblchh
Member

Откуда:
Сообщений: 6
Всем привет!
Прошу помощи в с запросом.
Из таблицы заказов Orders вынес все даты в таблицу OrderSteps, из-за того что пердполгалось, что количество этапов заказа и порядок может меняться, таблица Steps, с порядком сортировки, описанием.
Запрос, выполняется очень долго. Как оптимизировать, изменить?
Прикреплён файл с большим количеством данных для таблиц.
+

Талицы:
CREATE TABLE [dbo].[Orders](
	[OrderID] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [Orders$PrimaryKey] PRIMARY KEY CLUSTERED 
(
	[OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

SET IDENTITY_INSERT dbo.Orders ON
GO
INSERT dbo.Orders(OrderID) VALUES (188)
INSERT dbo.Orders(OrderID) VALUES (189)
INSERT dbo.Orders(OrderID) VALUES (190)
INSERT dbo.Orders(OrderID) VALUES (191)
INSERT dbo.Orders(OrderID) VALUES (192)
INSERT dbo.Orders(OrderID) VALUES (193)
INSERT dbo.Orders(OrderID) VALUES (194)
INSERT dbo.Orders(OrderID) VALUES (195)
INSERT dbo.Orders(OrderID) VALUES (196)
INSERT dbo.Orders(OrderID) VALUES (197)
INSERT dbo.Orders(OrderID) VALUES (198)
INSERT dbo.Orders(OrderID) VALUES (199)
INSERT dbo.Orders(OrderID) VALUES (200)
GO
SET IDENTITY_INSERT dbo.Orders OFF
GO

CREATE TABLE [dbo].[Steps](
	[StepID] [int] IDENTITY(1,1) NOT NULL,
	[SortOrder] [smallint] NULL,
	[StepDesc] [nvarchar](255) NULL,
 CONSTRAINT [Steps$PrimaryKey] PRIMARY KEY CLUSTERED 
(
	[StepID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SET IDENTITY_INSERT dbo.Steps ON
GO
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (1, 1, N'Tech pack sent')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (2, 2, N'Spec sent')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (3, 3, N'Block order sent')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (4, 5, N'1st sample received')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (5, 6, N'1st sample comments')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (6, 21, N'L/D colors sent to supplier for development')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (7, 22, N'L/D Colors received')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (8, 23, N'L/D colors comments')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (9, 7, N'P.P  Sample received')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (10, 8, N'P.P  Sample comments')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (11, 19, N'Final order page sent')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (13, 9, N'3rd sample received')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (14, 10, N'3rd sample comments')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (15, 11, N'Approved for production date ')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (16, 13, N'Production sample/photo samples  received date')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (17, 24, N'Starting production date')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (19, 14, N'Actual shipment date')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (20, 15, N'Estimated arrival date')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (21, 18, N'Kobi Order intention')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (23, 20, N'Care Labels sent')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (24, 16, N'FIRST SAMPLE FROM ANOTHER SUPPLIER')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (25, 17, N'FIRST SAMPLE COMMENTS FOR ANOTHER SUPPLIER')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (26, 12, N'Approved for production after photos')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (31, 25, N'Shipping Marks from Yael')
INSERT dbo.Steps(StepID, SortOrder, StepDesc) VALUES (32, 4, N'Confirmed price ')
GO
SET IDENTITY_INSERT dbo.Steps OFF
GO


CREATE TABLE [dbo].[OrderSteps](
	[OrderID] [int] NOT NULL,
	[StepID] [int] NOT NULL,
	[ExpectedDate] [datetime2](0) NULL,
	[ActualDate] [datetime2](0) NULL,
	[ID] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [OrderSteps$PrimaryKey] PRIMARY KEY CLUSTERED 
(
	[OrderID] ASC,
	[StepID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET IDENTITY_INSERT dbo.OrderSteps ON
GO
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 1, '2018-05-14 00:00:00.0000000', '2018-05-14 00:00:00.0000000', 1996)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 1997)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 1998)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 4, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 1999)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 5, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2000)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 6, NULL, NULL, 2001)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 7, NULL, NULL, 2002)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 8, NULL, NULL, 2003)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 9, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2004)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 10, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2005)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 11, '2018-08-21 11:06:07.0000000', '2018-09-13 10:46:44.0000000', 2006)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2007)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2008)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 15, '2018-08-13 11:39:18.0000000', '2018-08-13 11:39:18.0000000', 2009)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 16, '2018-09-13 10:46:44.0000000', NULL, 2010)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 17, '2018-09-12 11:39:18.0000000', NULL, 2011)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 19, NULL, NULL, 3765)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 20, NULL, NULL, 3766)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8707)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 23, NULL, NULL, 10970)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 24, NULL, NULL, 11836)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 25, NULL, NULL, 12378)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 26, '2018-11-19 11:49:22.0000000', NULL, 13932)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 31, NULL, NULL, 21455)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (188, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22635)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 1, '2018-06-14 00:00:00.0000000', '2018-06-14 00:00:00.0000000', 2012)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2013)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2014)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 4, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2015)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 5, '2018-08-13 09:14:01.0000000', '2018-08-13 09:27:17.0000000', 2016)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 6, NULL, NULL, 2017)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 7, NULL, NULL, 2018)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 8, NULL, NULL, 2019)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 9, '2018-08-20 09:27:17.0000000', '0001-01-01 00:00:00.0000000', 2020)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 10, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2021)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 11, '2018-08-13 11:15:56.0000000', '2018-08-13 11:15:56.0000000', 2022)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2023)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2024)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 15, '2018-08-13 09:14:26.0000000', '2018-08-13 09:27:31.0000000', 2025)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 16, '2019-01-03 11:49:22.0000000', NULL, 2026)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 17, '2018-09-12 09:27:31.0000000', NULL, 2027)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 19, NULL, NULL, 3767)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 20, NULL, NULL, 3768)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8708)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 23, NULL, NULL, 10971)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 24, NULL, NULL, 11837)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 25, NULL, NULL, 12379)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 26, NULL, NULL, 13933)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 31, NULL, NULL, 21456)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (189, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22636)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 1, '2018-06-18 00:00:00.0000000', '2018-06-18 00:00:00.0000000', 2028)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2029)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 3, '2018-06-24 00:00:00.0000000', '2018-06-24 00:00:00.0000000', 2030)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 4, '2018-10-27 11:32:41.0000000', '2018-07-30 00:00:00.0000000', 2031)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 5, '2018-08-27 10:26:34.0000000', '2018-08-27 10:26:34.0000000', 2032)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 6, NULL, NULL, 2033)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 7, NULL, NULL, 2034)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 8, NULL, NULL, 2035)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 9, '2018-09-19 11:10:47.0000000', '2018-10-02 00:00:00.0000000', 2036)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 10, '2018-10-09 00:00:00.0000000', '2018-10-22 00:00:00.0000000', 2037)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 11, '2018-10-22 00:00:00.0000000', '2018-11-13 14:41:20.0000000', 2038)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 13, '2018-11-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2039)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2040)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 15, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2041)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 16, '2018-12-19 11:48:37.0000000', NULL, 2042)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 17, NULL, NULL, 2043)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 19, NULL, NULL, 3769)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 20, NULL, NULL, 3770)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 21, '2018-07-23 13:37:36.0000000', '2018-10-15 11:32:41.0000000', 8709)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 23, '2018-08-27 10:26:34.0000000', NULL, 10972)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 24, NULL, NULL, 11838)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 25, NULL, NULL, 12380)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 26, '2018-10-22 00:00:00.0000000', '2018-10-22 00:00:00.0000000', 13934)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 31, '2018-11-13 14:41:20.0000000', '2018-11-13 14:41:00.0000000', 21457)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (190, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22637)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 1, '2018-06-18 00:00:00.0000000', '2018-06-18 00:00:00.0000000', 2044)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2045)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 3, '2018-06-24 00:00:00.0000000', '2018-06-24 00:00:00.0000000', 2046)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 4, '2018-07-30 00:00:00.0000000', '2018-07-30 00:00:00.0000000', 2047)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 5, '2018-08-28 00:00:00.0000000', '2018-08-28 00:00:00.0000000', 2048)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 6, NULL, NULL, 2049)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 7, NULL, NULL, 2050)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 8, NULL, NULL, 2051)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 9, '2018-09-19 11:10:47.0000000', '2018-10-02 00:00:00.0000000', 2052)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 10, '2018-10-09 00:00:00.0000000', '2018-10-21 00:00:00.0000000', 2053)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 11, '2018-10-21 00:00:00.0000000', '2018-11-13 14:41:16.0000000', 2054)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 13, '2018-11-29 11:49:21.0000000', '0001-01-01 00:00:00.0000000', 2055)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2056)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 15, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2057)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 16, '2018-12-19 10:48:53.0000000', NULL, 2058)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 17, NULL, NULL, 2059)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 19, NULL, NULL, 3771)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 20, NULL, NULL, 3772)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 21, '2018-09-04 12:07:34.0000000', '2018-09-04 12:07:34.0000000', 8710)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 23, '2018-08-28 00:00:00.0000000', NULL, 10973)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 24, NULL, NULL, 11839)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 25, NULL, NULL, 12381)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 26, '2018-10-21 00:00:00.0000000', '2018-10-21 00:00:00.0000000', 13935)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 31, '2018-11-13 14:41:16.0000000', '2018-11-13 14:41:00.0000000', 21458)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (191, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22638)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 1, '2018-07-22 12:22:52.0000000', '2018-07-22 12:22:52.0000000', 2060)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 2, '2018-09-26 11:10:48.0000000', '0001-01-01 00:00:00.0000000', 2061)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2062)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 4, '2018-08-29 15:42:28.0000000', '2018-08-29 15:42:28.0000000', 2063)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 5, '2018-09-10 15:42:28.0000000', '0001-01-01 00:00:00.0000000', 2064)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 6, NULL, NULL, 2065)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 7, NULL, NULL, 2066)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 8, NULL, NULL, 2067)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 9, '2018-08-29 15:42:59.0000000', '2018-08-29 15:42:59.0000000', 2068)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 10, '2018-09-12 00:00:00.0000000', '2018-09-20 16:52:15.0000000', 2069)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 11, NULL, '2018-11-26 15:22:50.0000000', 2070)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 13, '2018-09-30 00:00:00.0000000', '2018-11-12 00:00:00.0000000', 2071)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 14, '2018-11-19 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2072)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 15, '2018-09-20 00:00:00.0000000', '2018-09-20 16:52:15.0000000', 2073)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 16, '2018-11-04 13:01:55.0000000', NULL, 2074)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 17, NULL, NULL, 2075)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 19, NULL, NULL, 3773)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 20, NULL, NULL, 3774)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8711)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 23, '2018-11-26 15:22:50.0000000', NULL, 10974)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 24, NULL, NULL, 11840)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 25, NULL, NULL, 12382)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 26, '2018-09-20 16:52:15.0000000', NULL, 13936)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 31, '2018-11-26 15:22:46.0000000', '2018-11-26 15:22:46.0000000', 21459)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (192, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22639)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 1, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2076)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 2, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2077)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2078)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 4, '2018-09-16 00:00:00.0000000', '2018-09-16 00:00:00.0000000', 2079)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 5, '2018-09-23 00:00:00.0000000', '2018-10-15 08:36:31.0000000', 2080)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 6, NULL, NULL, 2081)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 7, NULL, NULL, 2082)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 8, NULL, NULL, 2083)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 9, '2018-12-01 11:49:22.0000000', '0001-01-01 00:00:00.0000000', 2084)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 10, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2085)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 11, NULL, '2018-11-26 15:21:51.0000000', 2086)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2087)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2088)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 15, '2018-10-15 00:00:00.0000000', '2018-10-15 00:00:00.0000000', 2089)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 16, '2018-11-29 12:37:02.0000000', NULL, 2090)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 17, NULL, NULL, 2091)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 19, NULL, NULL, 3775)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 20, NULL, NULL, 3776)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8712)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 23, '2018-11-26 15:21:51.0000000', NULL, 10975)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 24, NULL, NULL, 11841)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 25, NULL, NULL, 12383)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 26, '2018-10-15 00:00:00.0000000', NULL, 13937)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 31, '2018-11-26 15:21:40.0000000', '2018-11-26 15:21:40.0000000', 21460)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (193, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22640)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 1, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2092)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 2, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2093)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2094)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 4, '2018-09-16 00:00:00.0000000', '2018-09-16 00:00:00.0000000', 2095)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 5, '2018-09-23 00:00:00.0000000', '2018-10-15 08:40:23.0000000', 2096)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 6, NULL, NULL, 2097)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 7, NULL, NULL, 2098)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 8, NULL, NULL, 2099)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 9, '2018-12-01 11:49:22.0000000', '0001-01-01 00:00:00.0000000', 2100)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 10, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2101)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 11, NULL, '2018-11-26 15:21:48.0000000', 2102)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2103)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2104)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 15, '2018-10-15 00:00:00.0000000', '2018-10-15 00:00:00.0000000', 2105)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 16, '2018-11-29 12:37:02.0000000', NULL, 2106)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 17, NULL, NULL, 2107)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 19, NULL, NULL, 3777)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 20, NULL, NULL, 3778)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8713)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 23, '2018-11-26 15:21:48.0000000', NULL, 10976)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 24, NULL, NULL, 11842)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 25, NULL, NULL, 12384)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 26, '2018-10-15 00:00:00.0000000', NULL, 13938)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 31, '2018-11-26 15:21:40.0000000', '2018-11-26 15:21:40.0000000', 21461)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (194, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22641)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 1, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2108)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 2, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2109)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2110)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 4, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2111)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 5, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2112)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 6, NULL, NULL, 2113)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 7, NULL, NULL, 2114)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 8, NULL, NULL, 2115)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 9, '2018-08-29 15:42:59.0000000', '2018-08-29 15:42:59.0000000', 2116)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 10, '2018-09-12 00:00:00.0000000', '2018-09-20 00:00:00.0000000', 2117)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 11, '2018-09-20 00:00:00.0000000', '2018-11-26 15:21:44.0000000', 2118)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 13, '2018-09-30 00:00:00.0000000', '2018-11-12 00:00:00.0000000', 2119)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 14, '2018-11-19 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2120)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 15, '0001-01-01 00:00:00.0000000', '2018-11-14 00:00:00.0000000', 2121)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 16, '2018-12-29 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2122)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 17, NULL, NULL, 2123)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 19, NULL, NULL, 3779)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 20, NULL, NULL, 3780)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8714)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 23, '2018-11-26 15:21:44.0000000', NULL, 10977)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 24, NULL, NULL, 11843)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 25, NULL, NULL, 12385)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 26, '2018-09-20 00:00:00.0000000', '2018-09-20 00:00:00.0000000', 13939)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 31, '2018-11-26 15:21:40.0000000', '2018-11-26 15:21:40.0000000', 21462)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (195, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22642)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 1, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2124)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 2, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2125)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2126)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 4, '2018-10-27 00:00:00.0000000', '2018-10-27 00:00:00.0000000', 2127)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 5, '2018-11-03 00:00:00.0000000', '2018-11-14 00:00:00.0000000', 2128)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 6, NULL, NULL, 2129)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 7, NULL, NULL, 2130)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 8, NULL, NULL, 2131)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 9, '2018-11-26 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2132)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 10, '2018-09-13 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2133)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 11, NULL, '2018-11-18 15:24:01.0000000', 2134)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2135)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2136)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 15, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2137)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 16, '2018-12-30 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2138)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 17, NULL, NULL, 2139)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 19, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 3781)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 20, '2018-12-10 11:49:23.0000000', NULL, 3782)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 21, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 8715)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 23, NULL, NULL, 10978)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 24, NULL, NULL, 11844)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 25, NULL, NULL, 12386)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 26, '2018-11-15 00:00:00.0000000', '2018-11-15 00:00:00.0000000', 13940)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 31, '2018-11-18 15:24:01.0000000', NULL, 21463)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (196, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22643)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 1, '2018-06-18 00:00:00.0000000', '2018-06-18 00:00:00.0000000', 2140)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2141)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 3, '2018-06-24 00:00:00.0000000', '2018-06-24 00:00:00.0000000', 2142)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 4, '2018-07-30 00:00:00.0000000', '2018-07-30 00:00:00.0000000', 2143)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 5, '2018-09-24 11:10:47.0000000', '2018-08-27 00:00:00.0000000', 2144)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 6, NULL, NULL, 2145)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 7, NULL, NULL, 2146)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 8, NULL, NULL, 2147)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 9, '2018-09-19 11:10:47.0000000', '2018-10-02 00:00:00.0000000', 2148)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 10, '2018-10-09 00:00:00.0000000', NULL, 2149)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 11, NULL, NULL, 2150)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 13, NULL, NULL, 2151)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 14, NULL, NULL, 2152)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 15, NULL, NULL, 2153)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 16, NULL, NULL, 2154)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 17, NULL, NULL, 2155)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 19, NULL, NULL, 3783)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 20, NULL, NULL, 3784)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 21, '2018-09-04 12:09:08.0000000', '2018-09-04 12:09:08.0000000', 8716)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 23, '2018-08-27 00:00:00.0000000', NULL, 10979)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 24, NULL, NULL, 11845)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 25, NULL, NULL, 12387)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 26, NULL, NULL, 13941)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 31, NULL, NULL, 21464)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (197, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22644)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 1, '2018-06-18 00:00:00.0000000', '2018-06-18 00:00:00.0000000', 2156)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2157)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 3, '2018-06-24 00:00:00.0000000', '2018-06-24 00:00:00.0000000', 2158)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 4, '2018-10-27 12:50:30.0000000', '2018-07-30 00:00:00.0000000', 2159)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 5, '2018-09-24 00:00:00.0000000', '2018-09-20 00:00:00.0000000', 2160)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 6, NULL, NULL, 2161)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 7, NULL, NULL, 2162)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 8, NULL, NULL, 2163)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 9, '2018-10-02 00:00:00.0000000', '2018-10-24 00:00:00.0000000', 2164)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 10, '2018-10-31 00:00:00.0000000', '2018-10-24 00:00:00.0000000', 2165)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 11, NULL, '2018-11-13 14:41:11.0000000', 2166)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 13, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2167)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2168)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 15, '2018-10-24 00:00:00.0000000', '2018-10-24 00:00:00.0000000', 2169)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 16, '2018-12-19 11:04:55.0000000', NULL, 2170)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 17, NULL, NULL, 2171)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 19, NULL, NULL, 3785)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 20, NULL, NULL, 3786)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 21, '2018-07-23 13:37:36.0000000', '2018-10-15 12:50:30.0000000', 8717)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 23, NULL, NULL, 10980)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 24, NULL, NULL, 11846)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 25, NULL, NULL, 12388)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 26, '2018-10-24 00:00:00.0000000', NULL, 13942)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 31, '2018-11-13 14:41:11.0000000', '2018-11-13 14:41:00.0000000', 21465)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (198, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22645)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 1, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2172)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 2, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2173)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 3, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2174)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 4, '2018-10-28 08:59:06.0000000', '2018-08-12 07:22:58.0000000', 2175)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 5, '2018-08-24 00:00:00.0000000', '2018-08-30 00:00:00.0000000', 2176)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 6, NULL, NULL, 2177)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 7, NULL, NULL, 2178)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 8, NULL, NULL, 2179)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 9, '2018-09-06 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2180)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 10, '2018-10-16 00:00:00.0000000', '2018-10-16 00:00:00.0000000', 2181)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 11, '2018-10-17 00:00:00.0000000', '2018-10-21 11:18:22.0000000', 2182)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 13, '2018-10-26 00:00:00.0000000', NULL, 2183)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 14, NULL, NULL, 2184)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 15, NULL, NULL, 2185)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 16, '2018-12-05 11:18:22.0000000', NULL, 2186)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 17, NULL, NULL, 2187)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 19, NULL, NULL, 3787)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 20, NULL, NULL, 3788)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 21, '0001-01-01 00:00:00.0000000', '2018-10-16 08:59:06.0000000', 8718)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 23, '2018-08-29 07:43:12.0000000', NULL, 10981)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 24, NULL, NULL, 11847)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 25, NULL, NULL, 12389)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 26, NULL, NULL, 13943)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 31, NULL, NULL, 21466)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (199, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22646)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 1, '2018-06-18 00:00:00.0000000', '2018-06-18 00:00:00.0000000', 2188)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 2, '2018-09-26 11:10:47.0000000', '0001-01-01 00:00:00.0000000', 2189)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 3, '2018-06-24 00:00:00.0000000', '2018-06-24 00:00:00.0000000', 2190)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 4, '2018-10-27 11:32:41.0000000', '2018-07-30 00:00:00.0000000', 2191)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 5, '2018-08-19 11:22:20.0000000', '2018-08-19 11:22:20.0000000', 2192)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 6, NULL, NULL, 2193)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 7, NULL, NULL, 2194)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 8, NULL, NULL, 2195)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 9, '2018-08-26 11:22:20.0000000', '2018-10-02 00:00:00.0000000', 2196)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 10, '2018-10-09 00:00:00.0000000', '2018-10-22 00:00:00.0000000', 2197)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 11, '2018-10-22 00:00:00.0000000', '2018-11-13 14:41:05.0000000', 2198)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 13, '2018-11-29 11:49:21.0000000', '0001-01-01 00:00:00.0000000', 2199)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 14, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2200)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 15, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 2201)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 16, '2018-12-19 12:03:52.0000000', NULL, 2202)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 17, NULL, NULL, 2203)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 19, NULL, NULL, 3789)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 20, NULL, NULL, 3790)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 21, '2018-07-23 13:37:36.0000000', '2018-10-15 11:32:41.0000000', 8719)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 23, NULL, NULL, 10982)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 24, NULL, NULL, 11848)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 25, NULL, NULL, 12390)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 26, '2018-10-22 00:00:00.0000000', '2018-10-22 00:00:00.0000000', 13944)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 31, '2018-11-13 14:41:05.0000000', '2018-11-13 14:41:00.0000000', 21467)
INSERT dbo.OrderSteps(OrderID, StepID, ExpectedDate, ActualDate, ID) VALUES (200, 32, '0001-01-01 00:00:00.0000000', '0001-01-01 00:00:00.0000000', 22647)
GO
SET IDENTITY_INSERT dbo.OrderSteps OFF
GO




Делаю такой запрос, выполняется очень долго.
SELECT        o.OrderID, s1.StepDesc AS StepDesc1, os1.ActualDate AS ActualDate1, os1.ExpectedDate AS ExpectedDate1, s2.StepDesc AS StepDesc2, os2.ActualDate AS ActualDate2, os2.ExpectedDate AS ExpectedDate2, 
                         s3.StepDesc AS StepDesc3, os3.ActualDate AS ActualDate3, os3.ExpectedDate AS ExpectedDate3, s4.StepDesc AS StepDesc4, os4.ActualDate AS ActualDate4, os4.ExpectedDate AS ExpectedDate4, s5.StepDesc AS StepDesc5, 
                         os5.ActualDate AS ActualDate5, os5.ExpectedDate AS ExpectedDate5, s6.StepDesc AS StepDesc6, os6.ActualDate AS ActualDate6, os6.ExpectedDate AS ExpectedDate6, s7.StepDesc AS StepDesc7, 
                         os7.ActualDate AS ActualDate7, os7.ExpectedDate AS ExpectedDate7, s8.StepDesc AS StepDesc8, os8.ActualDate AS ActualDate8, os8.ExpectedDate AS ExpectedDate8, s9.StepDesc AS StepDesc9, 
                         os9.ActualDate AS ActualDate9, os9.ExpectedDate AS ExpectedDate9, s10.StepDesc AS StepDesc10, os10.ActualDate AS ActualDate10, os10.ExpectedDate AS ExpectedDate10, s11.StepDesc AS StepDesc11, 
                         os11.ActualDate AS ActualDate11, os11.ExpectedDate AS ExpectedDate11, s12.StepDesc AS StepDesc12, os12.ActualDate AS ActualDate12, os12.ExpectedDate AS ExpectedDate12, s13.StepDesc AS StepDesc13, 
                         os13.ActualDate AS ActualDate13, os13.ExpectedDate AS ExpectedDate13, s14.StepDesc AS StepDesc14, os14.ActualDate AS ActualDate14, os14.ExpectedDate AS ExpectedDate14, s15.StepDesc AS StepDesc15, 
                         os15.ActualDate AS ActualDate15, os15.ExpectedDate AS ExpectedDate15, s18.StepDesc AS StepDesc18, os18.ActualDate AS ActualDate18, os18.ExpectedDate AS ExpectedDate18, s19.StepDesc AS StepDesc19, 
                         os19.ActualDate AS ActualDate19, os19.ExpectedDate AS ExpectedDate19, s20.StepDesc AS StepDesc20, os20.ActualDate AS ActualDate20, os20.ExpectedDate AS ExpectedDate20, s21.StepDesc AS StepDesc21, 
                         os21.ActualDate AS ActualDate21, os21.ExpectedDate AS ExpectedDate21, s22.StepDesc AS StepDesc22, os22.ActualDate AS ActualDate22, os22.ExpectedDate AS ExpectedDate22, s23.StepDesc AS StepDesc23, 
                         os23.ActualDate AS ActualDate23, os23.ExpectedDate AS ExpectedDate23, s24.StepDesc AS StepDesc24, os24.ActualDate AS ActualDate24, os24.ExpectedDate AS ExpectedDate24
FROM            dbo.Orders AS o INNER JOIN
                         dbo.OrderSteps AS os1 ON o.OrderID = os1.OrderID INNER JOIN
                         dbo.Steps AS s1 ON os1.StepID = s1.StepID INNER JOIN
                         dbo.OrderSteps AS os2 ON o.OrderID = os2.OrderID INNER JOIN
                         dbo.Steps AS s2 ON os2.StepID = s2.StepID INNER JOIN
                         dbo.OrderSteps AS os3 ON o.OrderID = os3.OrderID INNER JOIN
                         dbo.Steps AS s3 ON os3.StepID = s3.StepID INNER JOIN
                         dbo.OrderSteps AS os4 ON o.OrderID = os4.OrderID INNER JOIN
                         dbo.Steps AS s4 ON os4.StepID = s4.StepID INNER JOIN
                         dbo.OrderSteps AS os5 ON o.OrderID = os5.OrderID INNER JOIN
                         dbo.Steps AS s5 ON os5.StepID = s5.StepID INNER JOIN
                         dbo.OrderSteps AS os6 ON o.OrderID = os6.OrderID INNER JOIN
                         dbo.Steps AS s6 ON os6.StepID = s6.StepID INNER JOIN
                         dbo.OrderSteps AS os7 ON o.OrderID = os7.OrderID INNER JOIN
                         dbo.Steps AS s7 ON os7.StepID = s7.StepID INNER JOIN
                         dbo.OrderSteps AS os8 ON o.OrderID = os8.OrderID INNER JOIN
                         dbo.Steps AS s8 ON os8.StepID = s8.StepID INNER JOIN
                         dbo.OrderSteps AS os9 ON o.OrderID = os9.OrderID INNER JOIN
                         dbo.Steps AS s9 ON os9.StepID = s9.StepID INNER JOIN
                         dbo.OrderSteps AS os10 ON o.OrderID = os10.OrderID INNER JOIN
                         dbo.Steps AS s10 ON os10.StepID = s10.StepID INNER JOIN
                         dbo.OrderSteps AS os11 ON o.OrderID = os11.OrderID INNER JOIN
                         dbo.Steps AS s11 ON os11.StepID = s11.StepID INNER JOIN
                         dbo.OrderSteps AS os12 ON o.OrderID = os12.OrderID INNER JOIN
                         dbo.Steps AS s12 ON os12.StepID = s12.StepID INNER JOIN
                         dbo.OrderSteps AS os13 ON o.OrderID = os13.OrderID INNER JOIN
                         dbo.Steps AS s13 ON os13.StepID = s13.StepID INNER JOIN
                         dbo.OrderSteps AS os14 ON o.OrderID = os14.OrderID INNER JOIN
                         dbo.Steps AS s14 ON os14.StepID = s14.StepID INNER JOIN
                         dbo.OrderSteps AS os15 ON o.OrderID = os15.OrderID INNER JOIN
                         dbo.Steps AS s15 ON os15.StepID = s15.StepID INNER JOIN
                         dbo.OrderSteps AS os18 ON o.OrderID = os18.OrderID INNER JOIN
                         dbo.Steps AS s18 ON os18.StepID = s18.StepID INNER JOIN
                         dbo.OrderSteps AS os19 ON o.OrderID = os19.OrderID INNER JOIN
                         dbo.Steps AS s19 ON os19.StepID = s19.StepID INNER JOIN
                         dbo.OrderSteps AS os20 ON o.OrderID = os20.OrderID INNER JOIN
                         dbo.Steps AS s20 ON os20.StepID = s20.StepID INNER JOIN
                         dbo.OrderSteps AS os21 ON o.OrderID = os21.OrderID INNER JOIN
                         dbo.Steps AS s21 ON os21.StepID = s21.StepID INNER JOIN
                         dbo.OrderSteps AS os22 ON o.OrderID = os22.OrderID INNER JOIN
                         dbo.Steps AS s22 ON os22.StepID = s22.StepID INNER JOIN
                         dbo.OrderSteps AS os23 ON o.OrderID = os23.OrderID INNER JOIN
                         dbo.Steps AS s23 ON os23.StepID = s23.StepID INNER JOIN
                         dbo.OrderSteps AS os24 ON o.OrderID = os24.OrderID INNER JOIN
                         dbo.Steps AS s24 ON os24.StepID = s24.StepID
WHERE        (s1.SortOrder = 1) AND (s2.SortOrder = 2) AND (s3.SortOrder = 3) AND (s4.SortOrder = 4) AND (s5.SortOrder = 5) AND (s6.SortOrder = 6) AND (s7.SortOrder = 7) AND (s8.SortOrder = 8) AND (s9.SortOrder = 9) AND 
                         (s10.SortOrder = 10) AND (s11.SortOrder = 11) AND (s12.SortOrder = 12) AND (s13.SortOrder = 13) AND (s14.SortOrder = 14) AND (s15.SortOrder = 15) AND (s18.SortOrder = 18) AND (s19.SortOrder = 19) AND (s20.SortOrder = 20) 
                         AND (s21.SortOrder = 21) AND (s22.SortOrder = 22) AND (s23.SortOrder = 23) AND (s24.SortOrder = 24)

Модератор: В следующий раз поротянки поедут в корзину, а не под тег spoiler


К сообщению приложен файл (sql.zip - 115Kb) cкачать
4 дек 18, 13:46    [21752881]     Ответить | Цитировать Сообщить модератору
 Re: Помощь с запросом  [new]
Владислав Колосов
Member

Откуда:
Сообщений: 5951
romblchh,

похоже, что Вам Pivot нужен или динамический Pivot.
4 дек 18, 15:21    [21753081]     Ответить | Цитировать Сообщить модератору
 Re: Помощь с запросом  [new]
a_voronin
Member

Откуда: Москва
Сообщений: 3706
[quot romblchh]Как оптимизировать, изменить? [/spoiler]

Сделать OLAP куб
4 дек 18, 15:29    [21753103]     Ответить | Цитировать Сообщить модератору
 Re: Помощь с запросом  [new]
court
Member

Откуда:
Сообщений: 1521
Владислав Колосов
romblchh,

похоже, что Вам Pivot нужен или динамический Pivot.
+1

romblchh,

select 
		o.OrderID

		,max(case when s.SortOrder = 1 then s.StepDesc end) AS StepDesc1
		,max(case when s.SortOrder = 1 then os.ActualDate end) AS ActualDate1
		,max(case when s.SortOrder = 1 then os.ExpectedDate end) AS ExpectedDate1

		,max(case when s.SortOrder = 2 then s.StepDesc end) AS StepDesc2
		,max(case when s.SortOrder = 2 then os.ActualDate end) AS ActualDate2
		,max(case when s.SortOrder = 2 then os.ExpectedDate end) AS ExpectedDate2

		,max(case when s.SortOrder = 3 then s.StepDesc end) AS StepDesc3
		,max(case when s.SortOrder = 3 then os.ActualDate end) AS ActualDate3
		,max(case when s.SortOrder = 3 then os.ExpectedDate end) AS ExpectedDate3

		,max(case when s.SortOrder = 4 then s.StepDesc end) AS StepDesc4
		,max(case when s.SortOrder = 4 then os.ActualDate end) AS ActualDate4
		,max(case when s.SortOrder = 4 then os.ExpectedDate end) AS ExpectedDate4

		,max(case when s.SortOrder = 5 then s.StepDesc end) AS StepDesc5
		,max(case when s.SortOrder = 5 then os.ActualDate end) AS ActualDate5
		,max(case when s.SortOrder = 5 then os.ExpectedDate end) AS ExpectedDate5

		,max(case when s.SortOrder = 6 then s.StepDesc end) AS StepDesc6
		,max(case when s.SortOrder = 6 then os.ActualDate end) AS ActualDate6
		,max(case when s.SortOrder = 6 then os.ExpectedDate end) AS ExpectedDate6

		,max(case when s.SortOrder = 7 then s.StepDesc end) AS StepDesc7
		,max(case when s.SortOrder = 7 then os.ActualDate end) AS ActualDate7
		,max(case when s.SortOrder = 7 then os.ExpectedDate end) AS ExpectedDate7

		,max(case when s.SortOrder = 8 then s.StepDesc end) AS StepDesc8
		,max(case when s.SortOrder = 8 then os.ActualDate end) AS ActualDate8
		,max(case when s.SortOrder = 8 then os.ExpectedDate end) AS ExpectedDate8

		/* ... */

		,max(case when s.SortOrder = 24 then s.StepDesc end) AS StepDesc24
		,max(case when s.SortOrder = 24 then os.ActualDate end) AS ActualDate24
		,max(case when s.SortOrder = 24 then os.ExpectedDate end) AS ExpectedDate24

FROM            dbo.Orders AS o 
INNER JOIN		dbo.OrderSteps AS os ON o.OrderID = os.OrderID 
INNER JOIN		dbo.Steps AS s ON os.StepID = s.StepID

group by o.OrderID
4 дек 18, 15:36    [21753120]     Ответить | Цитировать Сообщить модератору
 Re: Помощь с запросом  [new]
romblchh
Member

Откуда:
Сообщений: 6
court,
Огромное спасибо, очень помогли.
И всем кто откликнулся спасибо.
4 дек 18, 16:07    [21753231]     Ответить | Цитировать Сообщить модератору
Все форумы / Microsoft SQL Server Ответить