text: RENAME OBJECT [FACTINTERNETSALES] TO [FACTINTERNETSALES_OLD]
  GO
  
  
  CREATE TABLE [DBO].[FACTINTERNETSALES]
  (
  	[PRODUCTKEY] [INT] NOT NULL,
  	[ORDERDATEKEY] [INT] NOT NULL,
  	[DUEDATEKEY] [INT] NOT NULL,
  	[SHIPDATEKEY] [INT] NOT NULL,
  	[CUSTOMERKEY] [INT] NOT NULL,
  	[PROMOTIONKEY] [INT] NOT NULL,
  	[CURRENCYKEY] [INT] NOT NULL,
  	[SALESTERRITORYKEY] [INT] NOT NULL,
  	[SALESORDERNUMBER] NVARCHAR(20) NOT NULL,
  	[SALESORDERLINENUMBER] [SMALLINT] NOT NULL,
  	[REVISIONNUMBER] [TINYINT] NOT NULL,
  	[ORDERQUANTITY] [SMALLINT] NOT NULL,
  	[UNITPRICE] [MONEY] NOT NULL,
  	[EXTENDEDAMOUNT] [MONEY] NOT NULL,
  	[UNITPRICEDISCOUNTPCT] [FLOAT] NOT NULL,
  	[DISCOUNTAMOUNT] [FLOAT] NOT NULL,
  	[PRODUCTSTANDARDCOST] [MONEY] NOT NULL,
  	[TOTALPRODUCTCOST] [MONEY] NOT NULL,
  	[SALESAMOUNT] [MONEY] NOT NULL,
  	[TAXAMT] [MONEY] NOT NULL,
  	[FREIGHT] [MONEY] NOT NULL,
  	[CARRIERTRACKINGNUMBER] [NVARCHAR](25) NULL,
  	[CUSTOMERPONUMBER] [NVARCHAR](25) NULL
  )
  WITH
  (
  	DISTRIBUTION = HASH ( [PRODUCTKEY] ),
  	CLUSTERED COLUMNSTORE INDEX
  )
  GO
  
  DECLARE @I INT = 1
  DECLARE @BASEDATE DATETIME2 = '2001/07/01' --(SELECT MIN(STARTDATE) FROM DIMPRODUCT)--'20190410' --'2001/07/01' <- ORIGINAL ONE.
  
  --DECLARE @ORDERS TINYINT
  DECLARE @TOPPRODUCT SMALLINT
  DECLARE @TOPCUSTOMER SMALLINT
  
  DECLARE @REVISIONNUMBER TINYINT
  DECLARE @PROMOTIONKEY INT
  DECLARE @CURRENCYKEY TINYINT
  DECLARE @SALESTERRITORYKEY SMALLINT
  
  WHILE (DATEADD(DAY,@I,@BASEDATE)  < GETDATE())
  BEGIN
  	
  	--SET @ORDERS = (SELECT ABS(CHECKSUM(NEWID())) % 5 + 1)
  	--SET @TOPCURRENCY = (SELECT ABS(CHECKSUM(NEWID())) % 5 + 1)
  	--SET @TOPTERRITORY = (SELECT  ABS(CHECKSUM(NEWID())) % 5 + 1)
  	SET @TOPPRODUCT = (SELECT ABS(CHECKSUM(NEWID())) % 200 + 1)
  	SET @TOPCUSTOMER = (SELECT ABS(CHECKSUM(NEWID())) % 550 + 1)
  	SET @REVISIONNUMBER = (SELECT ABS(CHECKSUM(NEWID())) % 4 + 1)
  	SET @SALESTERRITORYKEY = (SELECT ABS(CHECKSUM(NEWID())) % 10 + 1)
  
  	SET @PROMOTIONKEY = (SELECT TOP 1 PROMOTIONKEY FROM DIMPROMOTION WHERE DATEADD(DAY,@I,@BASEDATE) >= STARTDATE AND DATEADD(DAY,@I,@BASEDATE) < ISNULL(ENDDATE,'99991231') AND ENGLISHPROMOTIONNAME LIKE 'NO DISCOUNT%')
  	SET @CURRENCYKEY = (SELECT TOP 1 CURRENCYKEY FROM DIMCURRENCY ORDER BY NEWID())
  	--SET @SALESTERRITORYKEY = (SELECT TOP 1 SALESTERRITORYKEY FROM DIMSALESTERRITORY ORDER BY NEWID())
  
  	;WITH CUSTPROD AS 
  	(
  		SELECT DISTINCT CUSTOMERKEY
  			, PRODUCTKEY
  			, CONVERT(VARCHAR(8), DATEADD(DAY, 1, DATEADD(DAY,@I,@BASEDATE)), 112) [ORDERDATEKEY]
  			, CONVERT(VARCHAR(8), DATEADD(DAY, 2, DATEADD(DAY,@I,@BASEDATE)), 112)  [DUEDATEKEY]
  			, CONVERT(VARCHAR(8), DATEADD(DAY, 3, DATEADD(DAY,@I,@BASEDATE)), 112) [SHIPDATEKEY]
  			, @REVISIONNUMBER [REVISIONNUMBER]
  			, @PROMOTIONKEY [PROMOTIONKEY]
  			, 'SO' + CAST(CUSTOMERKEY AS VARCHAR(5))+ CONVERT(VARCHAR(8), DATEADD(DAY, @I, @BASEDATE), 112) +  CAST(DENSE_RANK() OVER(ORDER BY CUSTOMERKEY) AS VARCHAR(20)) [SALESORDERNUMBER]
  			, @CURRENCYKEY CURRENCYKEY
  			, @SALESTERRITORYKEY SALESTERRITORYKEY
  		FROM
  		(
  			--PRODUCT
  			SELECT TOP(@TOPPRODUCT) PRODUCTKEY FROM DIMPRODUCT WHERE DATEADD(DAY,@I,@BASEDATE) >= STARTDATE AND DATEADD(DAY,@I,@BASEDATE) < ISNULL(ENDDATE,'99991231')
  		)A,
  		(
  			SELECT TOP(@TOPCUSTOMER) CUSTOMERKEY FROM DIMCUSTOMER WHERE DATEADD(DAY,@I,@BASEDATE) >= DATEFIRSTPURCHASE
  		)B
  
  	)
  	,CUSTPRODDETAILS AS
  	(
  
  		SELECT 
  			PD.[PRODUCTKEY]
  			, [CUSTOMERKEY]
  			, [PROMOTIONKEY]
  			, [ORDERDATEKEY]
  			, [DUEDATEKEY]
  			, [SHIPDATEKEY]
  			, CURRENCYKEY
  			, SALESTERRITORYKEY
  			, [SALESORDERNUMBER]
  			--, COUNT(*)  OVER(PARTITION BY CUSTOMERKEY) [SALESORDERLINENUMBER]
  			, ROW_NUMBER() OVER(PARTITION BY CUSTOMERKEY ORDER BY PD.PRODUCTKEY) [SALESORDERLINENUMBER]
  			, [REVISIONNUMBER]
  			, ABS(CHECKSUM(NEWID())) % 5 + 1 [ORDERQUANTITY]
  			, ISNULL(PD.LISTPRICE,0) [UNITPRICE]
  			--, [EXTENDEDAMOUNT]
  			,0 [UNITPRICEDISCOUNTPCT]
  			--, [DISCOUNTAMOUNT]
  			, ISNULL(PD.STANDARDCOST,0) [PRODUCTSTANDARDCOST]
  			--, [TOTALPRODUCTCOST]
  			--, [SALESAMOUNT]
  			, ABS(CHECKSUM(NEWID())) % 10.00 + 1 [TAXAMT]
  			, ABS(CHECKSUM(NEWID())) % 8.00 + 1 [FREIGHT]
  			, NULL [CARRIERTRACKINGNUMBER]
  			, NULL [CUSTOMERPONUMBER]
  		FROM CUSTPROD P
  			INNER JOIN DIMPRODUCT PD
  				ON P.PRODUCTKEY = PD.PRODUCTKEY
  	--GROUP BY P.CUSTOMERKEY, PD.[PRODUCTKEY],  PD.LISTPRICE, PD.STANDARDCOST
  	)
  	INSERT INTO FACTINTERNETSALES
  	SELECT 
  		[PRODUCTKEY]
  		, [ORDERDATEKEY]
  		, [DUEDATEKEY]
  		, [SHIPDATEKEY]
  		, [CUSTOMERKEY]
  		, [PROMOTIONKEY]
  		, [CURRENCYKEY]
  		, [SALESTERRITORYKEY]
  		, [SALESORDERNUMBER]
  		, [SALESORDERLINENUMBER]
  		, [REVISIONNUMBER]
  		, [ORDERQUANTITY]
  		, [UNITPRICE]
  		, [ORDERQUANTITY]*[UNITPRICE] [EXTENDEDAMOUNT]
  		, [UNITPRICEDISCOUNTPCT]
  		, 0 [DISCOUNTAMOUNT]
  		, [PRODUCTSTANDARDCOST]
  		, [PRODUCTSTANDARDCOST] * [ORDERQUANTITY] [TOTALPRODUCTCOST]
  		, [ORDERQUANTITY]*[UNITPRICE]  [SALESAMOUNT]
  		, [TAXAMT]
  		, [FREIGHT]
  		, [CARRIERTRACKINGNUMBER]
  		, [CUSTOMERPONUMBER]
  	FROM CUSTPRODDETAILS
  
  	SET @I+=1
  
  END
  