The objective is to have a function that can split a source table base on given conditions.Could have multiple application as in ETL data processing , partitioning or creating streams of data based on given conditions .splitSQL [table , { name1 condition1, name2 condition2 , ...} ]The input is a list or association of conditions based on columns of the table.The output is an association of tables with same structures as source table and containing rows filtered on corresponding condition
Load SQL functions
Load SQL functions
Load the last version of SQLOperators package containing SQL function developed so far.
See my previous post : SQL operators as Wolfram Language functions (https://community.wolfram.com/groups/-/m/t/2195893)
See my previous post : SQL operators as Wolfram Language functions (https://community.wolfram.com/groups/-/m/t/2195893)
CloudImport["https://www.wolframcloud.com/env/damcalrom/Published/SQLOperators.wl"];
Generate sample sql table
Generate sample sql table
In[]:=
symbols={"MSFT","TSLA","AMZN"};sampledata=(Flatten/@Tuples[{{#},FinancialData[#,{{2020}}]//Normal}])&/@symbols//Flatten[#,1]&;sampledataSQL=tableSQL[sampledata,{"symbol","time","close"}];sampledataSQL//showTableSQL
Total Rows:759 Elapsed:1.×
-7
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Using splitSQL
Using splitSQL
Split sample data into 2 separate outputs with based on simple criteria
In[]:=
tbsplit= sampledataSQL // splitSQL [# , "output1" symbol"TSLA" , "output2" symbol"MSFT" ] &;//AbsoluteTimingtbsplit["output1"]//showTableSQLtbsplit["output2"]//showTableSQL
Out[]=
{0.0065403,Null}
Total Rows:253 Elapsed:1.×
-6
10
Out[]//TableForm=
symbol | time | close |
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA |
Total Rows:253 Elapsed:2.5×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Split table on multiple conditions and add rows not matching any specify conditions in separate stream.
rowsLeft is a special keyword that will include all rows not matching any of specified conditions
rowsLeft is a special keyword that will include all rows not matching any of specified conditions
In[]:=
tbsplit=sampledataSQL// splitSQL[#,"202001NotAMZN"DateValue[time,{"Year","Month"}]{2020,1}&&symbol≠"AMZN", "202002"DateValue[time,{"Year","Month"}]{2020,2}, "RowsNotMach"rowsLeft]&;//AbsoluteTimingtbsplit["202001NotAMZN"]//showTableSQLtbsplit["202002"]//showTableSQLtbsplit["RowsNotMach"]//showTableSQL
Out[]=
{0.248863,Null}
Total Rows:42 Elapsed:1.×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Total Rows:57 Elapsed:1.9×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Total Rows:660 Elapsed:2.3×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Generate a list of multiple conditions based on monthly date range. Useful to partition table by month.
Export table to multiple files based on condition