divide by zero error even after predicate filters zero values inside cte with LEAD function

  • Hello community!

    The following code has baffled me and my friend (he found this). Sample data is attached as txt file as well.

    Sample data

    IF OBJECT_ID('dbo.msft') IS NOT NULL

    DROP TABLE [dbo].[msft]

    GO

    CREATE TABLE [dbo].[msft](

    [Date] [date] NULL,

    [Open] [decimal](6, 2) NULL,

    [High] [decimal](6, 2) NULL,

    [Low] [decimal](6, 2) NULL,

    [Close] [decimal](6, 2) NULL,

    [Volume] [int] NULL

    ) ON [PRIMARY]

    GO

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-31' AS Date), CAST(52.26 AS Decimal(6, 2)), CAST(53.00 AS Decimal(6, 2)), CAST(52.08 AS Decimal(6, 2)), CAST(53.00 AS Decimal(6, 2)), 37653081)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-27' AS Date), CAST(51.92 AS Decimal(6, 2)), CAST(52.32 AS Decimal(6, 2)), CAST(51.77 AS Decimal(6, 2)), CAST(52.32 AS Decimal(6, 2)), 17653660)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-26' AS Date), CAST(51.93 AS Decimal(6, 2)), CAST(51.98 AS Decimal(6, 2)), CAST(51.36 AS Decimal(6, 2)), CAST(51.89 AS Decimal(6, 2)), 24182929)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-25' AS Date), CAST(51.92 AS Decimal(6, 2)), CAST(52.49 AS Decimal(6, 2)), CAST(51.79 AS Decimal(6, 2)), CAST(52.12 AS Decimal(6, 2)), 24040186)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-24' AS Date), CAST(50.70 AS Decimal(6, 2)), CAST(51.71 AS Decimal(6, 2)), CAST(50.40 AS Decimal(6, 2)), CAST(51.59 AS Decimal(6, 2)), 34621638)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-23' AS Date), CAST(50.60 AS Decimal(6, 2)), CAST(50.68 AS Decimal(6, 2)), CAST(49.98 AS Decimal(6, 2)), CAST(50.03 AS Decimal(6, 2)), 25999674)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-20' AS Date), CAST(50.48 AS Decimal(6, 2)), CAST(51.22 AS Decimal(6, 2)), CAST(50.40 AS Decimal(6, 2)), CAST(50.62 AS Decimal(6, 2)), 23692434)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-19' AS Date), CAST(50.47 AS Decimal(6, 2)), CAST(50.62 AS Decimal(6, 2)), CAST(49.82 AS Decimal(6, 2)), CAST(50.32 AS Decimal(6, 2)), 23722753)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-18' AS Date), CAST(50.48 AS Decimal(6, 2)), CAST(51.14 AS Decimal(6, 2)), CAST(50.30 AS Decimal(6, 2)), CAST(50.81 AS Decimal(6, 2)), 24803172)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-17' AS Date), CAST(51.72 AS Decimal(6, 2)), CAST(51.73 AS Decimal(6, 2)), CAST(50.36 AS Decimal(6, 2)), CAST(50.51 AS Decimal(6, 2)), 27766669)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-16' AS Date), CAST(50.80 AS Decimal(6, 2)), CAST(51.96 AS Decimal(6, 2)), CAST(50.75 AS Decimal(6, 2)), CAST(51.83 AS Decimal(6, 2)), 19944632)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-13' AS Date), CAST(51.44 AS Decimal(6, 2)), CAST(51.90 AS Decimal(6, 2)), CAST(51.04 AS Decimal(6, 2)), CAST(51.08 AS Decimal(6, 2)), 22436700)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-12' AS Date), CAST(51.20 AS Decimal(6, 2)), CAST(51.81 AS Decimal(6, 2)), CAST(50.92 AS Decimal(6, 2)), CAST(51.51 AS Decimal(6, 2)), 24099431)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-11' AS Date), CAST(51.13 AS Decimal(6, 2)), CAST(51.78 AS Decimal(6, 2)), CAST(51.00 AS Decimal(6, 2)), CAST(51.05 AS Decimal(6, 2)), 23910684)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-10' AS Date), CAST(50.33 AS Decimal(6, 2)), CAST(51.10 AS Decimal(6, 2)), CAST(50.19 AS Decimal(6, 2)), CAST(51.02 AS Decimal(6, 2)), 22741527)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-09' AS Date), CAST(50.49 AS Decimal(6, 2)), CAST(50.58 AS Decimal(6, 2)), CAST(50.00 AS Decimal(6, 2)), CAST(50.07 AS Decimal(6, 2)), 17844648)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-06' AS Date), CAST(49.92 AS Decimal(6, 2)), CAST(50.39 AS Decimal(6, 2)), CAST(49.66 AS Decimal(6, 2)), CAST(50.39 AS Decimal(6, 2)), 24715575)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-05' AS Date), CAST(49.87 AS Decimal(6, 2)), CAST(50.30 AS Decimal(6, 2)), CAST(49.73 AS Decimal(6, 2)), CAST(49.94 AS Decimal(6, 2)), 25309543)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-04' AS Date), CAST(49.84 AS Decimal(6, 2)), CAST(50.06 AS Decimal(6, 2)), CAST(49.46 AS Decimal(6, 2)), CAST(49.87 AS Decimal(6, 2)), 24171449)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-03' AS Date), CAST(50.34 AS Decimal(6, 2)), CAST(50.41 AS Decimal(6, 2)), CAST(49.60 AS Decimal(6, 2)), CAST(49.78 AS Decimal(6, 2)), 26196074)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-05-02' AS Date), CAST(50.00 AS Decimal(6, 2)), CAST(50.75 AS Decimal(6, 2)), CAST(49.78 AS Decimal(6, 2)), CAST(50.61 AS Decimal(6, 2)), 33004106)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-29' AS Date), CAST(49.35 AS Decimal(6, 2)), CAST(50.25 AS Decimal(6, 2)), CAST(49.35 AS Decimal(6, 2)), CAST(49.87 AS Decimal(6, 2)), 48339735)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-28' AS Date), CAST(50.62 AS Decimal(6, 2)), CAST(50.77 AS Decimal(6, 2)), CAST(49.56 AS Decimal(6, 2)), CAST(49.90 AS Decimal(6, 2)), 42758226)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-27' AS Date), CAST(51.48 AS Decimal(6, 2)), CAST(51.50 AS Decimal(6, 2)), CAST(50.55 AS Decimal(6, 2)), CAST(50.94 AS Decimal(6, 2)), 43145896)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-26' AS Date), CAST(52.26 AS Decimal(6, 2)), CAST(52.35 AS Decimal(6, 2)), CAST(51.09 AS Decimal(6, 2)), CAST(51.44 AS Decimal(6, 2)), 33262278)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-25' AS Date), CAST(51.78 AS Decimal(6, 2)), CAST(52.13 AS Decimal(6, 2)), CAST(51.63 AS Decimal(6, 2)), CAST(52.11 AS Decimal(6, 2)), 33106540)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-22' AS Date), CAST(51.91 AS Decimal(6, 2)), CAST(52.43 AS Decimal(6, 2)), CAST(50.77 AS Decimal(6, 2)), CAST(51.78 AS Decimal(6, 2)), 125600219)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-21' AS Date), CAST(55.80 AS Decimal(6, 2)), CAST(56.23 AS Decimal(6, 2)), CAST(55.42 AS Decimal(6, 2)), CAST(55.78 AS Decimal(6, 2)), 36826521)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-20' AS Date), CAST(56.29 AS Decimal(6, 2)), CAST(56.50 AS Decimal(6, 2)), CAST(55.48 AS Decimal(6, 2)), CAST(55.59 AS Decimal(6, 2)), 36014074)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-19' AS Date), CAST(56.63 AS Decimal(6, 2)), CAST(56.77 AS Decimal(6, 2)), CAST(55.68 AS Decimal(6, 2)), CAST(56.39 AS Decimal(6, 2)), 29533701)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-18' AS Date), CAST(55.49 AS Decimal(6, 2)), CAST(56.59 AS Decimal(6, 2)), CAST(55.21 AS Decimal(6, 2)), CAST(56.46 AS Decimal(6, 2)), 23150318)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-15' AS Date), CAST(55.30 AS Decimal(6, 2)), CAST(55.92 AS Decimal(6, 2)), CAST(55.11 AS Decimal(6, 2)), CAST(55.65 AS Decimal(6, 2)), 28377584)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-14' AS Date), CAST(55.22 AS Decimal(6, 2)), CAST(55.58 AS Decimal(6, 2)), CAST(55.07 AS Decimal(6, 2)), CAST(55.36 AS Decimal(6, 2)), 20875102)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-13' AS Date), CAST(55.12 AS Decimal(6, 2)), CAST(55.44 AS Decimal(6, 2)), CAST(54.89 AS Decimal(6, 2)), CAST(55.35 AS Decimal(6, 2)), 20601278)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-12' AS Date), CAST(54.37 AS Decimal(6, 2)), CAST(54.78 AS Decimal(6, 2)), CAST(53.76 AS Decimal(6, 2)), CAST(54.65 AS Decimal(6, 2)), 24574169)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-11' AS Date), CAST(54.49 AS Decimal(6, 2)), CAST(55.15 AS Decimal(6, 2)), CAST(54.30 AS Decimal(6, 2)), CAST(54.31 AS Decimal(6, 2)), 21332336)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-08' AS Date), CAST(54.67 AS Decimal(6, 2)), CAST(55.28 AS Decimal(6, 2)), CAST(54.32 AS Decimal(6, 2)), CAST(54.42 AS Decimal(6, 2)), 22157037)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-07' AS Date), CAST(54.87 AS Decimal(6, 2)), CAST(54.91 AS Decimal(6, 2)), CAST(54.23 AS Decimal(6, 2)), CAST(54.46 AS Decimal(6, 2)), 18584125)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-06' AS Date), CAST(54.36 AS Decimal(6, 2)), CAST(55.20 AS Decimal(6, 2)), CAST(54.21 AS Decimal(6, 2)), CAST(55.12 AS Decimal(6, 2)), 21032057)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-05' AS Date), CAST(55.19 AS Decimal(6, 2)), CAST(55.30 AS Decimal(6, 2)), CAST(54.46 AS Decimal(6, 2)), CAST(54.56 AS Decimal(6, 2)), 19148783)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-04' AS Date), CAST(55.43 AS Decimal(6, 2)), CAST(55.66 AS Decimal(6, 2)), CAST(55.00 AS Decimal(6, 2)), CAST(55.43 AS Decimal(6, 2)), 18832210)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-04-01' AS Date), CAST(55.05 AS Decimal(6, 2)), CAST(55.61 AS Decimal(6, 2)), CAST(54.57 AS Decimal(6, 2)), CAST(55.57 AS Decimal(6, 2)), 24298612)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-31' AS Date), CAST(54.95 AS Decimal(6, 2)), CAST(55.59 AS Decimal(6, 2)), CAST(54.86 AS Decimal(6, 2)), CAST(55.23 AS Decimal(6, 2)), 26173764)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-30' AS Date), CAST(54.93 AS Decimal(6, 2)), CAST(55.64 AS Decimal(6, 2)), CAST(54.90 AS Decimal(6, 2)), CAST(55.05 AS Decimal(6, 2)), 22920330)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-29' AS Date), CAST(53.66 AS Decimal(6, 2)), CAST(54.86 AS Decimal(6, 2)), CAST(53.45 AS Decimal(6, 2)), CAST(54.71 AS Decimal(6, 2)), 23375017)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-28' AS Date), CAST(54.21 AS Decimal(6, 2)), CAST(54.29 AS Decimal(6, 2)), CAST(53.33 AS Decimal(6, 2)), CAST(53.54 AS Decimal(6, 2)), 16988240)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-24' AS Date), CAST(53.84 AS Decimal(6, 2)), CAST(54.33 AS Decimal(6, 2)), CAST(53.73 AS Decimal(6, 2)), CAST(54.21 AS Decimal(6, 2)), 18842667)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-23' AS Date), CAST(54.11 AS Decimal(6, 2)), CAST(54.24 AS Decimal(6, 2)), CAST(53.74 AS Decimal(6, 2)), CAST(53.97 AS Decimal(6, 2)), 19905295)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-22' AS Date), CAST(53.61 AS Decimal(6, 2)), CAST(54.25 AS Decimal(6, 2)), CAST(53.46 AS Decimal(6, 2)), CAST(54.07 AS Decimal(6, 2)), 22911577)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-21' AS Date), CAST(53.25 AS Decimal(6, 2)), CAST(53.93 AS Decimal(6, 2)), CAST(52.93 AS Decimal(6, 2)), CAST(53.86 AS Decimal(6, 2)), 23807263)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-18' AS Date), CAST(54.92 AS Decimal(6, 2)), CAST(54.97 AS Decimal(6, 2)), CAST(53.44 AS Decimal(6, 2)), CAST(53.49 AS Decimal(6, 2)), 67422879)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-17' AS Date), CAST(54.21 AS Decimal(6, 2)), CAST(55.00 AS Decimal(6, 2)), CAST(54.00 AS Decimal(6, 2)), CAST(54.66 AS Decimal(6, 2)), 28186465)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-16' AS Date), CAST(53.45 AS Decimal(6, 2)), CAST(54.60 AS Decimal(6, 2)), CAST(53.40 AS Decimal(6, 2)), CAST(54.35 AS Decimal(6, 2)), 31297892)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-15' AS Date), CAST(52.75 AS Decimal(6, 2)), CAST(53.59 AS Decimal(6, 2)), CAST(52.74 AS Decimal(6, 2)), CAST(53.59 AS Decimal(6, 2)), 21104763)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-14' AS Date), CAST(52.71 AS Decimal(6, 2)), CAST(53.59 AS Decimal(6, 2)), CAST(52.63 AS Decimal(6, 2)), CAST(53.17 AS Decimal(6, 2)), 23751138)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-11' AS Date), CAST(53.00 AS Decimal(6, 2)), CAST(53.07 AS Decimal(6, 2)), CAST(52.38 AS Decimal(6, 2)), CAST(53.07 AS Decimal(6, 2)), 32174336)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-10' AS Date), CAST(52.93 AS Decimal(6, 2)), CAST(52.94 AS Decimal(6, 2)), CAST(51.16 AS Decimal(6, 2)), CAST(52.05 AS Decimal(6, 2)), 38384242)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-09' AS Date), CAST(51.89 AS Decimal(6, 2)), CAST(52.85 AS Decimal(6, 2)), CAST(51.86 AS Decimal(6, 2)), CAST(52.84 AS Decimal(6, 2)), 28145151)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-08' AS Date), CAST(50.80 AS Decimal(6, 2)), CAST(52.13 AS Decimal(6, 2)), CAST(50.60 AS Decimal(6, 2)), CAST(51.65 AS Decimal(6, 2)), 33352218)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-07' AS Date), CAST(51.56 AS Decimal(6, 2)), CAST(51.80 AS Decimal(6, 2)), CAST(50.58 AS Decimal(6, 2)), CAST(51.03 AS Decimal(6, 2)), 38407847)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-04' AS Date), CAST(52.40 AS Decimal(6, 2)), CAST(52.45 AS Decimal(6, 2)), CAST(51.71 AS Decimal(6, 2)), CAST(52.03 AS Decimal(6, 2)), 32280449)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-03' AS Date), CAST(52.97 AS Decimal(6, 2)), CAST(52.97 AS Decimal(6, 2)), CAST(51.78 AS Decimal(6, 2)), CAST(52.35 AS Decimal(6, 2)), 24418148)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-02' AS Date), CAST(52.41 AS Decimal(6, 2)), CAST(52.96 AS Decimal(6, 2)), CAST(52.16 AS Decimal(6, 2)), CAST(52.95 AS Decimal(6, 2)), 29049172)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-03-01' AS Date), CAST(50.97 AS Decimal(6, 2)), CAST(52.59 AS Decimal(6, 2)), CAST(50.92 AS Decimal(6, 2)), CAST(52.58 AS Decimal(6, 2)), 32778474)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-29' AS Date), CAST(51.35 AS Decimal(6, 2)), CAST(51.65 AS Decimal(6, 2)), CAST(50.66 AS Decimal(6, 2)), CAST(50.88 AS Decimal(6, 2)), 31247142)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-26' AS Date), CAST(52.60 AS Decimal(6, 2)), CAST(52.68 AS Decimal(6, 2)), CAST(51.10 AS Decimal(6, 2)), CAST(51.30 AS Decimal(6, 2)), 35869193)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-25' AS Date), CAST(51.73 AS Decimal(6, 2)), CAST(52.10 AS Decimal(6, 2)), CAST(50.61 AS Decimal(6, 2)), CAST(52.10 AS Decimal(6, 2)), 26695920)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-24' AS Date), CAST(50.69 AS Decimal(6, 2)), CAST(51.50 AS Decimal(6, 2)), CAST(50.20 AS Decimal(6, 2)), CAST(51.36 AS Decimal(6, 2)), 32076012)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-23' AS Date), CAST(52.34 AS Decimal(6, 2)), CAST(52.37 AS Decimal(6, 2)), CAST(50.98 AS Decimal(6, 2)), CAST(51.18 AS Decimal(6, 2)), 28390773)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-22' AS Date), CAST(52.28 AS Decimal(6, 2)), CAST(53.00 AS Decimal(6, 2)), CAST(52.28 AS Decimal(6, 2)), CAST(52.65 AS Decimal(6, 2)), 24854437)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-19' AS Date), CAST(51.97 AS Decimal(6, 2)), CAST(52.28 AS Decimal(6, 2)), CAST(51.53 AS Decimal(6, 2)), CAST(51.82 AS Decimal(6, 2)), 33275399)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-18' AS Date), CAST(52.33 AS Decimal(6, 2)), CAST(52.95 AS Decimal(6, 2)), CAST(52.10 AS Decimal(6, 2)), CAST(52.19 AS Decimal(6, 2)), 26244683)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-17' AS Date), CAST(51.49 AS Decimal(6, 2)), CAST(52.77 AS Decimal(6, 2)), CAST(51.45 AS Decimal(6, 2)), CAST(52.42 AS Decimal(6, 2)), 39670289)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-16' AS Date), CAST(50.90 AS Decimal(6, 2)), CAST(51.09 AS Decimal(6, 2)), CAST(50.13 AS Decimal(6, 2)), CAST(51.09 AS Decimal(6, 2)), 35937137)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-12' AS Date), CAST(50.25 AS Decimal(6, 2)), CAST(50.68 AS Decimal(6, 2)), CAST(49.75 AS Decimal(6, 2)), CAST(50.50 AS Decimal(6, 2)), 33292336)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-11' AS Date), CAST(48.68 AS Decimal(6, 2)), CAST(50.11 AS Decimal(6, 2)), CAST(48.51 AS Decimal(6, 2)), CAST(49.69 AS Decimal(6, 2)), 48448154)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-10' AS Date), CAST(49.89 AS Decimal(6, 2)), CAST(50.39 AS Decimal(6, 2)), CAST(49.52 AS Decimal(6, 2)), CAST(49.71 AS Decimal(6, 2)), 37899918)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-09' AS Date), CAST(49.02 AS Decimal(6, 2)), CAST(50.24 AS Decimal(6, 2)), CAST(48.67 AS Decimal(6, 2)), CAST(49.28 AS Decimal(6, 2)), 45822192)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-08' AS Date), CAST(49.55 AS Decimal(6, 2)), CAST(49.57 AS Decimal(6, 2)), CAST(48.19 AS Decimal(6, 2)), CAST(49.41 AS Decimal(6, 2)), 57006105)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-05' AS Date), CAST(51.94 AS Decimal(6, 2)), CAST(52.00 AS Decimal(6, 2)), CAST(49.56 AS Decimal(6, 2)), CAST(50.16 AS Decimal(6, 2)), 60651115)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-04' AS Date), CAST(52.10 AS Decimal(6, 2)), CAST(52.81 AS Decimal(6, 2)), CAST(51.37 AS Decimal(6, 2)), CAST(52.00 AS Decimal(6, 2)), 46803371)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-03' AS Date), CAST(53.25 AS Decimal(6, 2)), CAST(53.39 AS Decimal(6, 2)), CAST(51.26 AS Decimal(6, 2)), CAST(52.16 AS Decimal(6, 2)), 57347519)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-02' AS Date), CAST(54.17 AS Decimal(6, 2)), CAST(54.26 AS Decimal(6, 2)), CAST(52.65 AS Decimal(6, 2)), CAST(53.00 AS Decimal(6, 2)), 54453559)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-02-01' AS Date), CAST(54.88 AS Decimal(6, 2)), CAST(55.09 AS Decimal(6, 2)), CAST(54.50 AS Decimal(6, 2)), CAST(54.71 AS Decimal(6, 2)), 43587105)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-29' AS Date), CAST(54.73 AS Decimal(6, 2)), CAST(55.09 AS Decimal(6, 2)), CAST(54.00 AS Decimal(6, 2)), CAST(55.09 AS Decimal(6, 2)), 83137039)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-28' AS Date), CAST(51.86 AS Decimal(6, 2)), CAST(52.21 AS Decimal(6, 2)), CAST(51.25 AS Decimal(6, 2)), CAST(52.06 AS Decimal(6, 2)), 58489190)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-27' AS Date), CAST(52.01 AS Decimal(6, 2)), CAST(52.20 AS Decimal(6, 2)), CAST(51.02 AS Decimal(6, 2)), CAST(51.22 AS Decimal(6, 2)), 36266102)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-26' AS Date), CAST(51.79 AS Decimal(6, 2)), CAST(52.44 AS Decimal(6, 2)), CAST(51.55 AS Decimal(6, 2)), CAST(52.17 AS Decimal(6, 2)), 28699484)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-25' AS Date), CAST(51.94 AS Decimal(6, 2)), CAST(52.65 AS Decimal(6, 2)), CAST(51.65 AS Decimal(6, 2)), CAST(51.79 AS Decimal(6, 2)), 34497323)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-22' AS Date), CAST(51.41 AS Decimal(6, 2)), CAST(52.33 AS Decimal(6, 2)), CAST(51.26 AS Decimal(6, 2)), CAST(52.29 AS Decimal(6, 2)), 36878402)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-21' AS Date), CAST(51.00 AS Decimal(6, 2)), CAST(51.58 AS Decimal(6, 2)), CAST(50.30 AS Decimal(6, 2)), CAST(50.48 AS Decimal(6, 2)), 40169367)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-20' AS Date), CAST(49.98 AS Decimal(6, 2)), CAST(51.38 AS Decimal(6, 2)), CAST(49.10 AS Decimal(6, 2)), CAST(50.79 AS Decimal(6, 2)), 63162940)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-19' AS Date), CAST(51.48 AS Decimal(6, 2)), CAST(51.68 AS Decimal(6, 2)), CAST(50.06 AS Decimal(6, 2)), CAST(50.56 AS Decimal(6, 2)), 43128276)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-15' AS Date), CAST(51.31 AS Decimal(6, 2)), CAST(51.97 AS Decimal(6, 2)), CAST(50.34 AS Decimal(6, 2)), CAST(50.99 AS Decimal(6, 2)), 70739137)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-14' AS Date), CAST(52.00 AS Decimal(6, 2)), CAST(53.42 AS Decimal(6, 2)), CAST(51.57 AS Decimal(6, 2)), CAST(53.11 AS Decimal(6, 2)), 51429807)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-13' AS Date), CAST(53.80 AS Decimal(6, 2)), CAST(54.07 AS Decimal(6, 2)), CAST(51.30 AS Decimal(6, 2)), CAST(51.64 AS Decimal(6, 2)), 66119018)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-12' AS Date), CAST(52.76 AS Decimal(6, 2)), CAST(53.10 AS Decimal(6, 2)), CAST(52.06 AS Decimal(6, 2)), CAST(52.78 AS Decimal(6, 2)), 36095539)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-11' AS Date), CAST(52.51 AS Decimal(6, 2)), CAST(52.85 AS Decimal(6, 2)), CAST(51.46 AS Decimal(6, 2)), CAST(52.30 AS Decimal(6, 2)), 36943756)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-08' AS Date), CAST(52.37 AS Decimal(6, 2)), CAST(53.28 AS Decimal(6, 2)), CAST(52.15 AS Decimal(6, 2)), CAST(52.33 AS Decimal(6, 2)), 48753969)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-07' AS Date), CAST(52.70 AS Decimal(6, 2)), CAST(53.48 AS Decimal(6, 2)), CAST(52.07 AS Decimal(6, 2)), CAST(52.17 AS Decimal(6, 2)), 56564852)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-06' AS Date), CAST(54.32 AS Decimal(6, 2)), CAST(54.40 AS Decimal(6, 2)), CAST(53.64 AS Decimal(6, 2)), CAST(54.05 AS Decimal(6, 2)), 39518863)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-05' AS Date), CAST(54.93 AS Decimal(6, 2)), CAST(55.39 AS Decimal(6, 2)), CAST(54.54 AS Decimal(6, 2)), CAST(55.05 AS Decimal(6, 2)), 34079674)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2016-01-04' AS Date), CAST(54.32 AS Decimal(6, 2)), CAST(54.80 AS Decimal(6, 2)), CAST(53.39 AS Decimal(6, 2)), CAST(54.80 AS Decimal(6, 2)), 52843210)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-31' AS Date), CAST(56.04 AS Decimal(6, 2)), CAST(56.19 AS Decimal(6, 2)), CAST(55.42 AS Decimal(6, 2)), CAST(55.48 AS Decimal(6, 2)), 27334061)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-30' AS Date), CAST(56.47 AS Decimal(6, 2)), CAST(56.78 AS Decimal(6, 2)), CAST(56.29 AS Decimal(6, 2)), CAST(56.31 AS Decimal(6, 2)), 21704505)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-29' AS Date), CAST(56.29 AS Decimal(6, 2)), CAST(56.85 AS Decimal(6, 2)), CAST(56.06 AS Decimal(6, 2)), CAST(56.55 AS Decimal(6, 2)), 27731403)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-28' AS Date), CAST(55.35 AS Decimal(6, 2)), CAST(55.95 AS Decimal(6, 2)), CAST(54.98 AS Decimal(6, 2)), CAST(55.95 AS Decimal(6, 2)), 22458293)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-24' AS Date), CAST(55.86 AS Decimal(6, 2)), CAST(55.96 AS Decimal(6, 2)), CAST(55.43 AS Decimal(6, 2)), CAST(55.67 AS Decimal(6, 2)), 9570002)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-23' AS Date), CAST(55.70 AS Decimal(6, 2)), CAST(55.88 AS Decimal(6, 2)), CAST(55.44 AS Decimal(6, 2)), CAST(55.82 AS Decimal(6, 2)), 27279832)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-22' AS Date), CAST(54.99 AS Decimal(6, 2)), CAST(55.48 AS Decimal(6, 2)), CAST(54.50 AS Decimal(6, 2)), CAST(55.35 AS Decimal(6, 2)), 28322241)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-21' AS Date), CAST(54.88 AS Decimal(6, 2)), CAST(55.35 AS Decimal(6, 2)), CAST(54.23 AS Decimal(6, 2)), CAST(54.83 AS Decimal(6, 2)), 37246325)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-18' AS Date), CAST(54.78 AS Decimal(6, 2)), CAST(56.00 AS Decimal(6, 2)), CAST(54.03 AS Decimal(6, 2)), CAST(54.13 AS Decimal(6, 2)), 84684164)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-17' AS Date), CAST(56.36 AS Decimal(6, 2)), CAST(56.79 AS Decimal(6, 2)), CAST(55.53 AS Decimal(6, 2)), CAST(55.70 AS Decimal(6, 2)), 41280907)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-16' AS Date), CAST(55.54 AS Decimal(6, 2)), CAST(56.24 AS Decimal(6, 2)), CAST(54.76 AS Decimal(6, 2)), CAST(56.13 AS Decimal(6, 2)), 37503300)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-15' AS Date), CAST(55.66 AS Decimal(6, 2)), CAST(55.90 AS Decimal(6, 2)), CAST(55.09 AS Decimal(6, 2)), CAST(55.20 AS Decimal(6, 2)), 39842998)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-14' AS Date), CAST(54.33 AS Decimal(6, 2)), CAST(55.21 AS Decimal(6, 2)), CAST(53.68 AS Decimal(6, 2)), CAST(55.14 AS Decimal(6, 2)), 46768924)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-11' AS Date), CAST(54.71 AS Decimal(6, 2)), CAST(55.10 AS Decimal(6, 2)), CAST(54.01 AS Decimal(6, 2)), CAST(54.06 AS Decimal(6, 2)), 39549497)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-10' AS Date), CAST(55.39 AS Decimal(6, 2)), CAST(55.66 AS Decimal(6, 2)), CAST(55.01 AS Decimal(6, 2)), CAST(55.27 AS Decimal(6, 2)), 31775826)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-09' AS Date), CAST(54.75 AS Decimal(6, 2)), CAST(55.87 AS Decimal(6, 2)), CAST(54.51 AS Decimal(6, 2)), CAST(54.98 AS Decimal(6, 2)), 36373209)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-08' AS Date), CAST(55.47 AS Decimal(6, 2)), CAST(56.10 AS Decimal(6, 2)), CAST(54.99 AS Decimal(6, 2)), CAST(55.79 AS Decimal(6, 2)), 32878026)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-07' AS Date), CAST(55.79 AS Decimal(6, 2)), CAST(55.97 AS Decimal(6, 2)), CAST(55.29 AS Decimal(6, 2)), CAST(55.81 AS Decimal(6, 2)), 30709765)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-04' AS Date), CAST(54.12 AS Decimal(6, 2)), CAST(56.23 AS Decimal(6, 2)), CAST(54.10 AS Decimal(6, 2)), CAST(55.91 AS Decimal(6, 2)), 43963662)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-03' AS Date), CAST(55.49 AS Decimal(6, 2)), CAST(55.77 AS Decimal(6, 2)), CAST(53.93 AS Decimal(6, 2)), CAST(54.20 AS Decimal(6, 2)), 38627835)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-02' AS Date), CAST(55.32 AS Decimal(6, 2)), CAST(55.96 AS Decimal(6, 2)), CAST(55.06 AS Decimal(6, 2)), CAST(55.21 AS Decimal(6, 2)), 47244551)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-12-01' AS Date), CAST(54.41 AS Decimal(6, 2)), CAST(55.23 AS Decimal(6, 2)), CAST(54.30 AS Decimal(6, 2)), CAST(55.22 AS Decimal(6, 2)), 39848647)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-30' AS Date), CAST(54.54 AS Decimal(6, 2)), CAST(54.96 AS Decimal(6, 2)), CAST(54.00 AS Decimal(6, 2)), CAST(54.35 AS Decimal(6, 2)), 53885371)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-27' AS Date), CAST(53.80 AS Decimal(6, 2)), CAST(54.08 AS Decimal(6, 2)), CAST(53.79 AS Decimal(6, 2)), CAST(53.93 AS Decimal(6, 2)), 9009113)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-25' AS Date), CAST(54.09 AS Decimal(6, 2)), CAST(54.23 AS Decimal(6, 2)), CAST(53.69 AS Decimal(6, 2)), CAST(53.69 AS Decimal(6, 2)), 21005147)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-24' AS Date), CAST(53.92 AS Decimal(6, 2)), CAST(54.44 AS Decimal(6, 2)), CAST(53.58 AS Decimal(6, 2)), CAST(54.25 AS Decimal(6, 2)), 24482130)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-23' AS Date), CAST(54.25 AS Decimal(6, 2)), CAST(54.46 AS Decimal(6, 2)), CAST(53.75 AS Decimal(6, 2)), CAST(54.19 AS Decimal(6, 2)), 27767280)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-20' AS Date), CAST(54.25 AS Decimal(6, 2)), CAST(54.30 AS Decimal(6, 2)), CAST(53.26 AS Decimal(6, 2)), CAST(54.19 AS Decimal(6, 2)), 37100507)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-19' AS Date), CAST(53.99 AS Decimal(6, 2)), CAST(54.66 AS Decimal(6, 2)), CAST(53.78 AS Decimal(6, 2)), CAST(53.94 AS Decimal(6, 2)), 28131212)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-18' AS Date), CAST(53.00 AS Decimal(6, 2)), CAST(53.98 AS Decimal(6, 2)), CAST(52.98 AS Decimal(6, 2)), CAST(53.85 AS Decimal(6, 2)), 28794418)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-17' AS Date), CAST(53.17 AS Decimal(6, 2)), CAST(53.53 AS Decimal(6, 2)), CAST(52.85 AS Decimal(6, 2)), CAST(52.97 AS Decimal(6, 2)), 31211644)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-16' AS Date), CAST(53.08 AS Decimal(6, 2)), CAST(53.89 AS Decimal(6, 2)), CAST(52.85 AS Decimal(6, 2)), CAST(53.76 AS Decimal(6, 2)), 31610450)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-13' AS Date), CAST(53.07 AS Decimal(6, 2)), CAST(53.29 AS Decimal(6, 2)), CAST(52.53 AS Decimal(6, 2)), CAST(52.84 AS Decimal(6, 2)), 36399856)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-12' AS Date), CAST(53.48 AS Decimal(6, 2)), CAST(53.98 AS Decimal(6, 2)), CAST(53.19 AS Decimal(6, 2)), CAST(53.32 AS Decimal(6, 2)), 34485469)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-11' AS Date), CAST(53.70 AS Decimal(6, 2)), CAST(54.20 AS Decimal(6, 2)), CAST(53.46 AS Decimal(6, 2)), CAST(53.65 AS Decimal(6, 2)), 34004446)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-10' AS Date), CAST(54.07 AS Decimal(6, 2)), CAST(54.13 AS Decimal(6, 2)), CAST(53.27 AS Decimal(6, 2)), CAST(53.51 AS Decimal(6, 2)), 55020334)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-09' AS Date), CAST(54.55 AS Decimal(6, 2)), CAST(54.87 AS Decimal(6, 2)), CAST(53.56 AS Decimal(6, 2)), CAST(54.16 AS Decimal(6, 2)), 32466376)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-06' AS Date), CAST(54.08 AS Decimal(6, 2)), CAST(54.98 AS Decimal(6, 2)), CAST(53.96 AS Decimal(6, 2)), CAST(54.92 AS Decimal(6, 2)), 30488633)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-05' AS Date), CAST(54.49 AS Decimal(6, 2)), CAST(54.70 AS Decimal(6, 2)), CAST(54.00 AS Decimal(6, 2)), CAST(54.38 AS Decimal(6, 2)), 30648155)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-04' AS Date), CAST(54.18 AS Decimal(6, 2)), CAST(54.88 AS Decimal(6, 2)), CAST(54.06 AS Decimal(6, 2)), CAST(54.40 AS Decimal(6, 2)), 37020378)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-03' AS Date), CAST(52.93 AS Decimal(6, 2)), CAST(54.39 AS Decimal(6, 2)), CAST(52.90 AS Decimal(6, 2)), CAST(54.15 AS Decimal(6, 2)), 35842280)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-11-02' AS Date), CAST(52.85 AS Decimal(6, 2)), CAST(53.36 AS Decimal(6, 2)), CAST(52.62 AS Decimal(6, 2)), CAST(53.24 AS Decimal(6, 2)), 29946039)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-30' AS Date), CAST(53.32 AS Decimal(6, 2)), CAST(53.99 AS Decimal(6, 2)), CAST(52.62 AS Decimal(6, 2)), CAST(52.64 AS Decimal(6, 2)), 46473085)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-29' AS Date), CAST(53.54 AS Decimal(6, 2)), CAST(53.83 AS Decimal(6, 2)), CAST(53.22 AS Decimal(6, 2)), CAST(53.36 AS Decimal(6, 2)), 30036258)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-28' AS Date), CAST(53.54 AS Decimal(6, 2)), CAST(53.98 AS Decimal(6, 2)), CAST(52.86 AS Decimal(6, 2)), CAST(53.98 AS Decimal(6, 2)), 45353604)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-27' AS Date), CAST(53.99 AS Decimal(6, 2)), CAST(54.37 AS Decimal(6, 2)), CAST(53.58 AS Decimal(6, 2)), CAST(53.69 AS Decimal(6, 2)), 48640174)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-26' AS Date), CAST(52.53 AS Decimal(6, 2)), CAST(54.32 AS Decimal(6, 2)), CAST(52.50 AS Decimal(6, 2)), CAST(54.25 AS Decimal(6, 2)), 63486782)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-23' AS Date), CAST(52.30 AS Decimal(6, 2)), CAST(54.07 AS Decimal(6, 2)), CAST(52.25 AS Decimal(6, 2)), CAST(52.87 AS Decimal(6, 2)), 134828496)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-22' AS Date), CAST(47.53 AS Decimal(6, 2)), CAST(48.95 AS Decimal(6, 2)), CAST(47.09 AS Decimal(6, 2)), CAST(48.03 AS Decimal(6, 2)), 54435667)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-21' AS Date), CAST(47.92 AS Decimal(6, 2)), CAST(47.99 AS Decimal(6, 2)), CAST(47.11 AS Decimal(6, 2)), CAST(47.20 AS Decimal(6, 2)), 25144337)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-20' AS Date), CAST(47.44 AS Decimal(6, 2)), CAST(47.81 AS Decimal(6, 2)), CAST(47.02 AS Decimal(6, 2)), CAST(47.77 AS Decimal(6, 2)), 30573983)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-19' AS Date), CAST(47.42 AS Decimal(6, 2)), CAST(47.88 AS Decimal(6, 2)), CAST(47.02 AS Decimal(6, 2)), CAST(47.62 AS Decimal(6, 2)), 29106758)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-16' AS Date), CAST(47.02 AS Decimal(6, 2)), CAST(47.54 AS Decimal(6, 2)), CAST(46.90 AS Decimal(6, 2)), CAST(47.51 AS Decimal(6, 2)), 25779279)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-15' AS Date), CAST(47.01 AS Decimal(6, 2)), CAST(47.03 AS Decimal(6, 2)), CAST(46.53 AS Decimal(6, 2)), CAST(47.01 AS Decimal(6, 2)), 27118898)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-14' AS Date), CAST(46.65 AS Decimal(6, 2)), CAST(47.10 AS Decimal(6, 2)), CAST(46.53 AS Decimal(6, 2)), CAST(46.68 AS Decimal(6, 2)), 24645420)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-13' AS Date), CAST(46.56 AS Decimal(6, 2)), CAST(47.13 AS Decimal(6, 2)), CAST(46.56 AS Decimal(6, 2)), CAST(46.89 AS Decimal(6, 2)), 18937148)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-12' AS Date), CAST(46.98 AS Decimal(6, 2)), CAST(47.07 AS Decimal(6, 2)), CAST(46.50 AS Decimal(6, 2)), CAST(47.00 AS Decimal(6, 2)), 19734079)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-09' AS Date), CAST(47.45 AS Decimal(6, 2)), CAST(47.54 AS Decimal(6, 2)), CAST(46.92 AS Decimal(6, 2)), CAST(47.11 AS Decimal(6, 2)), 28397372)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-08' AS Date), CAST(46.56 AS Decimal(6, 2)), CAST(47.52 AS Decimal(6, 2)), CAST(46.50 AS Decimal(6, 2)), CAST(47.45 AS Decimal(6, 2)), 33679346)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-07' AS Date), CAST(47.10 AS Decimal(6, 2)), CAST(47.35 AS Decimal(6, 2)), CAST(45.95 AS Decimal(6, 2)), CAST(46.80 AS Decimal(6, 2)), 27381389)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-06' AS Date), CAST(46.33 AS Decimal(6, 2)), CAST(47.18 AS Decimal(6, 2)), CAST(46.22 AS Decimal(6, 2)), CAST(46.75 AS Decimal(6, 2)), 27017160)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-05' AS Date), CAST(45.74 AS Decimal(6, 2)), CAST(46.89 AS Decimal(6, 2)), CAST(45.70 AS Decimal(6, 2)), CAST(46.63 AS Decimal(6, 2)), 33015524)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-02' AS Date), CAST(44.27 AS Decimal(6, 2)), CAST(45.57 AS Decimal(6, 2)), CAST(43.92 AS Decimal(6, 2)), CAST(45.57 AS Decimal(6, 2)), 41571518)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-10-01' AS Date), CAST(44.75 AS Decimal(6, 2)), CAST(44.75 AS Decimal(6, 2)), CAST(43.75 AS Decimal(6, 2)), CAST(44.61 AS Decimal(6, 2)), 28470381)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-30' AS Date), CAST(43.88 AS Decimal(6, 2)), CAST(44.30 AS Decimal(6, 2)), CAST(43.66 AS Decimal(6, 2)), CAST(44.26 AS Decimal(6, 2)), 34589501)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-29' AS Date), CAST(43.37 AS Decimal(6, 2)), CAST(43.57 AS Decimal(6, 2)), CAST(43.05 AS Decimal(6, 2)), CAST(43.44 AS Decimal(6, 2)), 32653952)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-28' AS Date), CAST(43.83 AS Decimal(6, 2)), CAST(44.08 AS Decimal(6, 2)), CAST(43.20 AS Decimal(6, 2)), CAST(43.29 AS Decimal(6, 2)), 27219415)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-25' AS Date), CAST(44.48 AS Decimal(6, 2)), CAST(44.73 AS Decimal(6, 2)), CAST(43.76 AS Decimal(6, 2)), CAST(43.94 AS Decimal(6, 2)), 29177316)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-24' AS Date), CAST(43.45 AS Decimal(6, 2)), CAST(44.13 AS Decimal(6, 2)), CAST(43.27 AS Decimal(6, 2)), CAST(43.91 AS Decimal(6, 2)), 27652655)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-23' AS Date), CAST(43.93 AS Decimal(6, 2)), CAST(44.17 AS Decimal(6, 2)), CAST(43.51 AS Decimal(6, 2)), CAST(43.87 AS Decimal(6, 2)), 17057235)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-22' AS Date), CAST(43.38 AS Decimal(6, 2)), CAST(44.05 AS Decimal(6, 2)), CAST(43.31 AS Decimal(6, 2)), CAST(43.90 AS Decimal(6, 2)), 27824121)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-21' AS Date), CAST(43.62 AS Decimal(6, 2)), CAST(44.47 AS Decimal(6, 2)), CAST(43.60 AS Decimal(6, 2)), CAST(44.11 AS Decimal(6, 2)), 26075359)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-18' AS Date), CAST(43.50 AS Decimal(6, 2)), CAST(43.99 AS Decimal(6, 2)), CAST(43.33 AS Decimal(6, 2)), CAST(43.48 AS Decimal(6, 2)), 62221607)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-17' AS Date), CAST(44.29 AS Decimal(6, 2)), CAST(45.00 AS Decimal(6, 2)), CAST(44.08 AS Decimal(6, 2)), CAST(44.25 AS Decimal(6, 2)), 29817918)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-16' AS Date), CAST(43.97 AS Decimal(6, 2)), CAST(44.38 AS Decimal(6, 2)), CAST(43.84 AS Decimal(6, 2)), CAST(44.30 AS Decimal(6, 2)), 23256716)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-15' AS Date), CAST(43.19 AS Decimal(6, 2)), CAST(44.29 AS Decimal(6, 2)), CAST(43.08 AS Decimal(6, 2)), CAST(43.98 AS Decimal(6, 2)), 28662129)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-14' AS Date), CAST(43.43 AS Decimal(6, 2)), CAST(43.44 AS Decimal(6, 2)), CAST(42.86 AS Decimal(6, 2)), CAST(43.04 AS Decimal(6, 2)), 23633284)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-11' AS Date), CAST(43.14 AS Decimal(6, 2)), CAST(43.59 AS Decimal(6, 2)), CAST(42.94 AS Decimal(6, 2)), CAST(43.48 AS Decimal(6, 2)), 26184379)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-10' AS Date), CAST(43.12 AS Decimal(6, 2)), CAST(43.79 AS Decimal(6, 2)), CAST(42.74 AS Decimal(6, 2)), CAST(43.29 AS Decimal(6, 2)), 31234165)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-09' AS Date), CAST(44.21 AS Decimal(6, 2)), CAST(44.40 AS Decimal(6, 2)), CAST(42.91 AS Decimal(6, 2)), CAST(43.07 AS Decimal(6, 2)), 33318847)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-08' AS Date), CAST(43.30 AS Decimal(6, 2)), CAST(44.00 AS Decimal(6, 2)), CAST(43.20 AS Decimal(6, 2)), CAST(43.89 AS Decimal(6, 2)), 32204941)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-04' AS Date), CAST(42.81 AS Decimal(6, 2)), CAST(43.04 AS Decimal(6, 2)), CAST(42.20 AS Decimal(6, 2)), CAST(42.61 AS Decimal(6, 2)), 36771788)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-03' AS Date), CAST(43.41 AS Decimal(6, 2)), CAST(43.98 AS Decimal(6, 2)), CAST(43.28 AS Decimal(6, 2)), CAST(43.50 AS Decimal(6, 2)), 28053165)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-02' AS Date), CAST(42.36 AS Decimal(6, 2)), CAST(43.38 AS Decimal(6, 2)), CAST(41.88 AS Decimal(6, 2)), CAST(43.36 AS Decimal(6, 2)), 37490396)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-09-01' AS Date), CAST(42.17 AS Decimal(6, 2)), CAST(42.59 AS Decimal(6, 2)), CAST(41.66 AS Decimal(6, 2)), CAST(41.82 AS Decimal(6, 2)), 49194736)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-31' AS Date), CAST(43.56 AS Decimal(6, 2)), CAST(43.93 AS Decimal(6, 2)), CAST(43.10 AS Decimal(6, 2)), CAST(43.52 AS Decimal(6, 2)), 34159135)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-28' AS Date), CAST(43.40 AS Decimal(6, 2)), CAST(44.15 AS Decimal(6, 2)), CAST(43.39 AS Decimal(6, 2)), CAST(43.93 AS Decimal(6, 2)), 28104231)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-27' AS Date), CAST(43.23 AS Decimal(6, 2)), CAST(43.95 AS Decimal(6, 2)), CAST(42.92 AS Decimal(6, 2)), CAST(43.90 AS Decimal(6, 2)), 50498307)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-26' AS Date), CAST(42.01 AS Decimal(6, 2)), CAST(42.84 AS Decimal(6, 2)), CAST(41.06 AS Decimal(6, 2)), CAST(42.71 AS Decimal(6, 2)), 63031299)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-25' AS Date), CAST(42.56 AS Decimal(6, 2)), CAST(43.24 AS Decimal(6, 2)), CAST(40.39 AS Decimal(6, 2)), CAST(40.47 AS Decimal(6, 2)), 70239286)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-24' AS Date), CAST(40.45 AS Decimal(6, 2)), CAST(43.31 AS Decimal(6, 2)), CAST(39.72 AS Decimal(6, 2)), CAST(41.68 AS Decimal(6, 2)), 88049620)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-21' AS Date), CAST(45.30 AS Decimal(6, 2)), CAST(45.48 AS Decimal(6, 2)), CAST(43.07 AS Decimal(6, 2)), CAST(43.07 AS Decimal(6, 2)), 68835485)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-20' AS Date), CAST(46.07 AS Decimal(6, 2)), CAST(46.47 AS Decimal(6, 2)), CAST(45.66 AS Decimal(6, 2)), CAST(45.66 AS Decimal(6, 2)), 35671759)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-19' AS Date), CAST(46.78 AS Decimal(6, 2)), CAST(47.08 AS Decimal(6, 2)), CAST(46.30 AS Decimal(6, 2)), CAST(46.61 AS Decimal(6, 2)), 31485511)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-18' AS Date), CAST(46.84 AS Decimal(6, 2)), CAST(47.43 AS Decimal(6, 2)), CAST(46.70 AS Decimal(6, 2)), CAST(47.27 AS Decimal(6, 2)), 23574104)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-17' AS Date), CAST(46.81 AS Decimal(6, 2)), CAST(47.45 AS Decimal(6, 2)), CAST(46.57 AS Decimal(6, 2)), CAST(47.32 AS Decimal(6, 2)), 21099746)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-14' AS Date), CAST(46.53 AS Decimal(6, 2)), CAST(47.10 AS Decimal(6, 2)), CAST(46.52 AS Decimal(6, 2)), CAST(47.00 AS Decimal(6, 2)), 21473402)

    GO

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-13' AS Date), CAST(47.06 AS Decimal(6, 2)), CAST(47.10 AS Decimal(6, 2)), CAST(46.49 AS Decimal(6, 2)), CAST(46.73 AS Decimal(6, 2)), 22627241)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-12' AS Date), CAST(46.19 AS Decimal(6, 2)), CAST(46.90 AS Decimal(6, 2)), CAST(45.70 AS Decimal(6, 2)), CAST(46.74 AS Decimal(6, 2)), 30198532)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-11' AS Date), CAST(46.82 AS Decimal(6, 2)), CAST(46.94 AS Decimal(6, 2)), CAST(45.90 AS Decimal(6, 2)), CAST(46.41 AS Decimal(6, 2)), 29237402)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-10' AS Date), CAST(46.95 AS Decimal(6, 2)), CAST(47.49 AS Decimal(6, 2)), CAST(46.84 AS Decimal(6, 2)), CAST(47.33 AS Decimal(6, 2)), 23079870)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-07' AS Date), CAST(46.39 AS Decimal(6, 2)), CAST(46.78 AS Decimal(6, 2)), CAST(46.26 AS Decimal(6, 2)), CAST(46.74 AS Decimal(6, 2)), 19163038)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-06' AS Date), CAST(47.71 AS Decimal(6, 2)), CAST(47.77 AS Decimal(6, 2)), CAST(46.33 AS Decimal(6, 2)), CAST(46.62 AS Decimal(6, 2)), 27368000)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-05' AS Date), CAST(47.98 AS Decimal(6, 2)), CAST(48.41 AS Decimal(6, 2)), CAST(47.54 AS Decimal(6, 2)), CAST(47.58 AS Decimal(6, 2)), 26959655)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-04' AS Date), CAST(46.75 AS Decimal(6, 2)), CAST(47.71 AS Decimal(6, 2)), CAST(46.68 AS Decimal(6, 2)), CAST(47.54 AS Decimal(6, 2)), 33403916)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-08-03' AS Date), CAST(46.98 AS Decimal(6, 2)), CAST(47.00 AS Decimal(6, 2)), CAST(46.45 AS Decimal(6, 2)), CAST(46.81 AS Decimal(6, 2)), 24125886)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-31' AS Date), CAST(47.29 AS Decimal(6, 2)), CAST(47.37 AS Decimal(6, 2)), CAST(46.50 AS Decimal(6, 2)), CAST(46.70 AS Decimal(6, 2)), 31201480)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-30' AS Date), CAST(46.26 AS Decimal(6, 2)), CAST(47.40 AS Decimal(6, 2)), CAST(45.93 AS Decimal(6, 2)), CAST(46.88 AS Decimal(6, 2)), 39777883)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-29' AS Date), CAST(45.40 AS Decimal(6, 2)), CAST(46.78 AS Decimal(6, 2)), CAST(45.26 AS Decimal(6, 2)), CAST(46.29 AS Decimal(6, 2)), 40945864)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-28' AS Date), CAST(45.58 AS Decimal(6, 2)), CAST(45.64 AS Decimal(6, 2)), CAST(44.79 AS Decimal(6, 2)), CAST(45.34 AS Decimal(6, 2)), 34328948)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-27' AS Date), CAST(45.94 AS Decimal(6, 2)), CAST(46.01 AS Decimal(6, 2)), CAST(45.25 AS Decimal(6, 2)), CAST(45.35 AS Decimal(6, 2)), 39701420)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-24' AS Date), CAST(45.91 AS Decimal(6, 2)), CAST(46.32 AS Decimal(6, 2)), CAST(45.80 AS Decimal(6, 2)), CAST(45.94 AS Decimal(6, 2)), 32333244)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-23' AS Date), CAST(45.27 AS Decimal(6, 2)), CAST(46.23 AS Decimal(6, 2)), CAST(45.10 AS Decimal(6, 2)), CAST(46.11 AS Decimal(6, 2)), 33934010)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-22' AS Date), CAST(45.44 AS Decimal(6, 2)), CAST(46.93 AS Decimal(6, 2)), CAST(45.20 AS Decimal(6, 2)), CAST(45.54 AS Decimal(6, 2)), 59152427)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-21' AS Date), CAST(46.78 AS Decimal(6, 2)), CAST(47.33 AS Decimal(6, 2)), CAST(46.48 AS Decimal(6, 2)), CAST(47.28 AS Decimal(6, 2)), 42781899)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-20' AS Date), CAST(46.65 AS Decimal(6, 2)), CAST(47.13 AS Decimal(6, 2)), CAST(46.44 AS Decimal(6, 2)), CAST(46.92 AS Decimal(6, 2)), 30631905)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-17' AS Date), CAST(46.55 AS Decimal(6, 2)), CAST(46.78 AS Decimal(6, 2)), CAST(46.26 AS Decimal(6, 2)), CAST(46.62 AS Decimal(6, 2)), 29467107)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-16' AS Date), CAST(46.01 AS Decimal(6, 2)), CAST(46.69 AS Decimal(6, 2)), CAST(45.96 AS Decimal(6, 2)), CAST(46.66 AS Decimal(6, 2)), 26271673)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-15' AS Date), CAST(45.68 AS Decimal(6, 2)), CAST(45.89 AS Decimal(6, 2)), CAST(45.43 AS Decimal(6, 2)), CAST(45.76 AS Decimal(6, 2)), 26629615)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-14' AS Date), CAST(45.45 AS Decimal(6, 2)), CAST(45.96 AS Decimal(6, 2)), CAST(45.31 AS Decimal(6, 2)), CAST(45.62 AS Decimal(6, 2)), 22880260)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-13' AS Date), CAST(44.98 AS Decimal(6, 2)), CAST(45.62 AS Decimal(6, 2)), CAST(44.95 AS Decimal(6, 2)), CAST(45.54 AS Decimal(6, 2)), 28178329)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-10' AS Date), CAST(45.01 AS Decimal(6, 2)), CAST(45.14 AS Decimal(6, 2)), CAST(44.57 AS Decimal(6, 2)), CAST(44.61 AS Decimal(6, 2)), 25465757)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-09' AS Date), CAST(44.75 AS Decimal(6, 2)), CAST(45.22 AS Decimal(6, 2)), CAST(44.50 AS Decimal(6, 2)), CAST(44.52 AS Decimal(6, 2)), 32424672)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-08' AS Date), CAST(44.44 AS Decimal(6, 2)), CAST(44.90 AS Decimal(6, 2)), CAST(44.03 AS Decimal(6, 2)), CAST(44.24 AS Decimal(6, 2)), 39785901)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-07' AS Date), CAST(44.34 AS Decimal(6, 2)), CAST(44.49 AS Decimal(6, 2)), CAST(43.32 AS Decimal(6, 2)), CAST(44.30 AS Decimal(6, 2)), 36435827)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-06' AS Date), CAST(43.96 AS Decimal(6, 2)), CAST(44.48 AS Decimal(6, 2)), CAST(43.95 AS Decimal(6, 2)), CAST(44.39 AS Decimal(6, 2)), 23033961)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-02' AS Date), CAST(44.48 AS Decimal(6, 2)), CAST(44.75 AS Decimal(6, 2)), CAST(44.06 AS Decimal(6, 2)), CAST(44.40 AS Decimal(6, 2)), 21752013)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-07-01' AS Date), CAST(44.46 AS Decimal(6, 2)), CAST(45.23 AS Decimal(6, 2)), CAST(44.10 AS Decimal(6, 2)), CAST(44.44 AS Decimal(6, 2)), 28343907)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-30' AS Date), CAST(44.71 AS Decimal(6, 2)), CAST(44.72 AS Decimal(6, 2)), CAST(43.94 AS Decimal(6, 2)), CAST(44.15 AS Decimal(6, 2)), 35945384)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-29' AS Date), CAST(45.04 AS Decimal(6, 2)), CAST(45.23 AS Decimal(6, 2)), CAST(44.36 AS Decimal(6, 2)), CAST(44.37 AS Decimal(6, 2)), 34081746)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-26' AS Date), CAST(45.65 AS Decimal(6, 2)), CAST(46.28 AS Decimal(6, 2)), CAST(45.03 AS Decimal(6, 2)), CAST(45.26 AS Decimal(6, 2)), 49835287)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-25' AS Date), CAST(46.03 AS Decimal(6, 2)), CAST(46.06 AS Decimal(6, 2)), CAST(45.50 AS Decimal(6, 2)), CAST(45.65 AS Decimal(6, 2)), 20615995)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-24' AS Date), CAST(45.67 AS Decimal(6, 2)), CAST(46.24 AS Decimal(6, 2)), CAST(45.55 AS Decimal(6, 2)), CAST(45.64 AS Decimal(6, 2)), 34890901)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-23' AS Date), CAST(46.13 AS Decimal(6, 2)), CAST(46.28 AS Decimal(6, 2)), CAST(45.62 AS Decimal(6, 2)), CAST(45.91 AS Decimal(6, 2)), 25896473)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-22' AS Date), CAST(46.33 AS Decimal(6, 2)), CAST(46.72 AS Decimal(6, 2)), CAST(46.16 AS Decimal(6, 2)), CAST(46.23 AS Decimal(6, 2)), 20318055)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-19' AS Date), CAST(46.79 AS Decimal(6, 2)), CAST(46.82 AS Decimal(6, 2)), CAST(45.99 AS Decimal(6, 2)), CAST(46.10 AS Decimal(6, 2)), 63837036)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-18' AS Date), CAST(46.22 AS Decimal(6, 2)), CAST(46.80 AS Decimal(6, 2)), CAST(46.17 AS Decimal(6, 2)), CAST(46.72 AS Decimal(6, 2)), 32658343)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-17' AS Date), CAST(45.73 AS Decimal(6, 2)), CAST(46.07 AS Decimal(6, 2)), CAST(45.36 AS Decimal(6, 2)), CAST(45.97 AS Decimal(6, 2)), 28704061)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-16' AS Date), CAST(45.35 AS Decimal(6, 2)), CAST(46.24 AS Decimal(6, 2)), CAST(45.30 AS Decimal(6, 2)), CAST(45.83 AS Decimal(6, 2)), 27070306)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-15' AS Date), CAST(45.45 AS Decimal(6, 2)), CAST(45.65 AS Decimal(6, 2)), CAST(45.02 AS Decimal(6, 2)), CAST(45.48 AS Decimal(6, 2)), 33254517)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-12' AS Date), CAST(46.22 AS Decimal(6, 2)), CAST(46.47 AS Decimal(6, 2)), CAST(45.90 AS Decimal(6, 2)), CAST(45.97 AS Decimal(6, 2)), 23930972)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-11' AS Date), CAST(46.66 AS Decimal(6, 2)), CAST(46.92 AS Decimal(6, 2)), CAST(46.13 AS Decimal(6, 2)), CAST(46.44 AS Decimal(6, 2)), 27347758)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-10' AS Date), CAST(45.79 AS Decimal(6, 2)), CAST(46.83 AS Decimal(6, 2)), CAST(45.69 AS Decimal(6, 2)), CAST(46.61 AS Decimal(6, 2)), 28417405)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-09' AS Date), CAST(45.76 AS Decimal(6, 2)), CAST(45.94 AS Decimal(6, 2)), CAST(45.46 AS Decimal(6, 2)), CAST(45.65 AS Decimal(6, 2)), 24406057)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-08' AS Date), CAST(46.30 AS Decimal(6, 2)), CAST(46.43 AS Decimal(6, 2)), CAST(45.67 AS Decimal(6, 2)), CAST(45.73 AS Decimal(6, 2)), 22121634)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-05' AS Date), CAST(46.31 AS Decimal(6, 2)), CAST(46.52 AS Decimal(6, 2)), CAST(45.84 AS Decimal(6, 2)), CAST(46.14 AS Decimal(6, 2)), 25438086)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-04' AS Date), CAST(46.79 AS Decimal(6, 2)), CAST(47.16 AS Decimal(6, 2)), CAST(46.20 AS Decimal(6, 2)), CAST(46.36 AS Decimal(6, 2)), 27745512)

    INSERT [dbo].[msft] ([Date], [Open], [High], [Low], [Close], [Volume]) VALUES (CAST(N'2015-06-03' AS Date), CAST(47.37 AS Decimal(6, 2)), CAST(47.74 AS Decimal(6, 2)), CAST(46.82 AS Decimal(6, 2)), CAST(46.85 AS Decimal(6, 2)), 28002154)

    Query

    WITH CTE AS(

    select

    [Date],[Close], LEAD([Close],1,0) over(order by [date] desc) As PreviousDayClose

    from [dbo].[msft]

    ),

    CTE1 AS(

    SELECT *

    , ( ( [Close] - [PreviousDayClose] ) * 100.00 ) / (PreviousDayClose) As PercentChange

    FROM CTE

    where PreviousDayClose <> 0 -- problematic condition 1

    )

    SELECT * FROM CTE1

    where 1 =1

    and PercentChange > - 100.00 -- problematic condition 2

    order by PercentChange asc

    Error

    Msg 8134, Level 16, State 1, Line 1

    Divide by zero error encountered.

    The problem:

    Take a look at CTE1 , especially -- problematic condition 1 - it filters previousdayclose <> 0 , but still we get the error above. Our expectation was the division in SELECT should never have a zero as the divisor. I digged into the execution plan (attached) and found that the "PreviousDayClose" is getting calculated exclusively always and thats whats causing the issue.

    Now if i comment out -- problematic condition 2 in the final select, the query is executing flawlessly.

    Can you fine folks please shed some light on this? This happens on SQL 2012 and onwards.

  • No mistery here, just a minor mistake in the third parameter of the LEAD function which is specifying 0 as the missing value / default. Simply replace this with NULL to solve this problem.

    ๐Ÿ˜Ž

    Before

    LEAD([Close],1,0) over(order by [date] desc) As PreviousDayClose

    After

    LEAD([Close],1,NULL) over(order by [date] desc) As PreviousDayClose

  • Eirikur, thanks for the reply. I am still not convinced.

    I have added a condition PreviousdayClose <> 0 in CTE1 to exactly handle situations like this, where LEAD is seeding 0 for un-found values or if the real value in itself is 0. So in this case, the SELECT is getting calculated even before WHERE clause?

    BTW, i worked-around this issue by wrapping the whole LEAD function over NULLIF, which is what you provided you provided too, by replacing 0 with NULL in LEAD. But the core problem remains - why is SQL calculating SELECT before WHERE. and to add to mystery, if i comment out PercantChange > -100.00 , the divide-by-zero error doesnt occur.

  • Plamen Ratchev explains predicate evaluation order issues in this article[/url].

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply