create table [RedShiftResearch].[dbo].[RShField6](
	[QNum1] [int] NULL,
	[QNum2] [int] NULL,
	[RA_surface_ort_angle] [float] NULL,
	[DEC_surface_ort_angle] [float] NULL,
	[X_ort] [float] NULL,
	[Y_ort] [float] NULL,
	[Z_ort] [float] NULL,
	[Ort_sum] [float] NULL,
	[QuantityA] [int] NULL,
	[QuantityB] [int] NULL,
	[CheckQSum] [int] NULL,
	[RSh_sumA] [float] NULL,
	[RSh_sumB] [float] NULL,
	[RSh_sumCheck] [float] NULL,
	[RSh_midA] [float] NULL,
	[RSh_midB] [float] NULL,
	[RSh_dif] [float] NULL)
DECLARE @DIAPASON_L float = -3;
DECLARE @DIAPASON_H float = 20;
DECLARE @counter1 int = 0;
DECLARE @counter2 int = 0;
DECLARE @Q1 int;
DECLARE @Q2 int;
DECLARE @RA_surf_ort_angle float;
DECLARE @DEC_surf_ort_angle float;
DECLARE @X_ort float;
DECLARE @Y_ort float;
DECLARE @Z_ort float;
DECLARE @X_ort_neg float;
DECLARE @Y_ort_neg float;
DECLARE @Z_ort_neg float;
DECLARE @Ort_sum float;
DECLARE @RA_surf_step float = 5.0;
DECLARE @DEC_surf_step float = 5.0;
DECLARE @QuantityA int;
DECLARE @QuantityB int;
DECLARE @CheckQSum int;
DECLARE @RSh_sumA float;
DECLARE @RSh_sumB float;
DECLARE @RSh_sumCheck float;
DECLARE @RSh_midA float;
DECLARE @RSh_midB float;
DECLARE @RSh_dif float;
DECLARE @threshold float = 2.0;
WHILE (@counter1 < 19)
begin
	WHILE (@counter2 < 72)
	begin
		SET @Q1 = @counter1;
		SET @Q2 = @counter2;
		SET @RA_surf_ort_angle = @counter2 * @RA_surf_step;
		SET @DEC_surf_ort_angle = @counter1 * @DEC_surf_step;
		SET @Z_ort = SIN(@DEC_surf_ort_angle/180.0*PI());
		SET @X_ort = ROUND(COS(@RA_surf_ort_angle/180.0*PI())*COS(ASIN(@Z_ort)),15);
		SET @Y_ort = ROUND(SIN(@RA_surf_ort_angle/180.0*PI())*COS(ASIN(@Z_ort)),15);
		SET @X_ort_neg = -1 * @X_ort;
		SET @Y_ort_neg = -1 * @Y_ort;
		SET @Z_ort_neg = -1 * @Z_ort;
		SET @Ort_sum = @X_ort*@X_ort+@Y_ort*@Y_ort+@Z_ort*@Z_ort;
		SELECT @QuantityA = COUNT(*) FROM dbo.RSh8 where ([Z]>@DIAPASON_L) AND ([Z]<@DIAPASON_H) AND ([Z]<>0) AND ((SQUARE([Xd]+@X_ort)+SQUARE([Yd]+@Y_ort)+SQUARE([Zd]+@Z_ort))>@threshold);
		SELECT @QuantityB = COUNT(*) FROM dbo.RSh8 where ([Z]>@DIAPASON_L) AND ([Z]<@DIAPASON_H) AND ([Z]<>0) AND ((SQUARE([Xd]+@X_ort_neg)+SQUARE([Yd]+@Y_ort_neg)+SQUARE([Zd]+@Z_ort_neg))>@threshold);
		SET @CheckQSum = @QuantityA+@QuantityB;
		SELECT @RSh_sumA = SUM([Z]) FROM dbo.RSh8 where ([Z]>@DIAPASON_L) AND ([Z]<@DIAPASON_H) AND ([Z]<>0) AND ((SQUARE([Xd]+@X_ort)+SQUARE([Yd]+@Y_ort)+SQUARE([Zd]+@Z_ort))>@threshold);
		SELECT @RSh_sumB = SUM([Z]) FROM dbo.RSh8 where ([Z]>@DIAPASON_L) AND ([Z]<@DIAPASON_H) AND ([Z]<>0) AND ((SQUARE([Xd]+@X_ort_neg)+SQUARE([Yd]+@Y_ort_neg)+SQUARE([Zd]+@Z_ort_neg))>@threshold);
		SET @RSh_sumCheck = @RSh_sumA+@RSh_sumB;
		SET @RSh_midA = @RSh_sumA / @QuantityA;
		SET @RSh_midB = @RSh_sumB / @QuantityB;
		SET @RSh_dif = @RSh_midA - @RSh_midB;
	
		insert into RShField6(QNum1, QNum2, RA_surface_ort_angle, DEC_surface_ort_angle, X_ort, Y_ort, Z_ort, Ort_sum, QuantityA, QuantityB, CheckQSum, RSh_sumA, RSh_sumB, RSh_sumCheck, RSh_midA, RSh_midB, RSh_dif)
		values (@counter1,
		@counter2,
		@RA_surf_ort_angle,
		@DEC_surf_ort_angle,
		@X_ort,
		@Y_ort,
		@Z_ort,
		@Ort_sum,
		@QuantityA,
		@QuantityB,
		@CheckQSum,
		@RSh_sumA,
		@RSh_sumB,
		@RSh_sumCheck,
		@RSh_midA,
		@RSh_midB,
		@RSh_dif
		);
		
		set @counter2 = @counter2+1;
	end
	
	set @counter1 = @counter1+1;
	set @counter2 = 0;
end
select *
from [dbo].[RShField6];