--Three new keywords in Teradata Machine Learning Function syntax
	--ON
	--USING
	--DIMENSION

--and a required alias	

--ANSI SQL Syntax
	--The data source follows the FROM keyword

SELECT * FROM SalesOffload.sales_fact;

--Teradata Advanced SQL Function syntax
		--The Function follows the FROM keyword
		--The data source follows the ON keyword

SELECT * FROM Sessionize 
(ON SalesOffload.sales_fact
 PARTITION BY customer_id 
 ORDER BY sales_date 
 USING
 TimeColumn ('sales_date')
 TimeOut (86400)
) AS dt;

--Note the USING keyword that seperates Table arguments
	--from Function arguments
	
	--Order matters for Table arguments
	--Order does not matter for Function arguments
	
	--also notice the alias at the end 'AS dt'

SELECT * FROM Sessionize 
(ON SalesOffload.sales_fact
 PARTITION BY customer_id 
 ORDER BY sales_date 
 USING
 TimeColumn ('sales_date')
 TimeOut (86400)
) AS dt;

--You can filter your data souce as needed with SQL
SELECT * FROM SalesOffload.sales_fact WHERE customer_id IN (3154, 2287);

--You can see how the PARTITION BY and ORDER BY affect the rows
SELECT * FROM SalesOffload.sales_fact WHERE customer_id IN (3154, 2287) ORDER BY customer_id, sales_date;

SELECT * FROM Sessionize 
(ON (SELECT * FROM SalesOffload.sales_fact WHERE customer_id IN (3154, 2287))
 PARTITION BY customer_id 
 ORDER BY sales_date 
 USING
 TimeColumn ('sales_date')
 TimeOut (86400)
) AS dt;

--The last new keyword is DIMENSION

--Create tables we will use in next example

CREATE SET TABLE sales_table
     (
      user_id INTEGER,
      event VARCHAR(12),
      ts TIMESTAMP(6)     
     );    
  
INSERT sales_table (1,'activity','2017-01-01 13:21:09.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:10.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:11.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:12.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:13.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:14.000000');
INSERT sales_table (1,'activity','2017-01-01 13:21:15.000000');
INSERT sales_table (1,'buy','2017-01-01 13:21:16.000000');

CREATE SET TABLE conversion_event_table
	(
	conversion_events VARCHAR (255)
	);

INSERT conversion_event_table ('buy');

CREATE SET TABLE model1_table
	(
	id integer, 
	model varchar (255)
	);

INSERT model1_table(0, 'SEGMENT_ROWS');
INSERT model1_table(1,'4,ALL:1.0:UNIFORM:NA');

--Look at Sales Table and DIMENSION tables
SELECT * FROM sales_table;
SELECT * FROM conversion_event_table;
SELECT * FROM model1_table;

SELECT * FROM Attribution
(ON sales_table PARTITION BY user_id ORDER BY ts
 ON convert_table AS conversion DIMENSION
 ON model1_table AS model1 DIMENSION 
 USING
 EventColumn ('event')
 TimestampColumn ('ts')
 WindowSize ('rows:4')
) AS dt ORDER BY user_id, ts;