-
Notifications
You must be signed in to change notification settings - Fork 360
How to execute SQL on CSV files with esProc SPL
esProc SPL supports simple SQL and can directly execute SQL statements on structured text files such as CSV, allowing for SQL calculations without the need for a database. Download esProc SPL first: https://www.esproc.com/download-esproc/ If you don't want to deal with the source code, you can simply download and install the standard edition. Prepare a CSV file of standard format.

$select * from d:/Orders.csv
Note that the preceding $ indicates that this statement is SQL, otherwise it is assumed to be a native SPL statement by default. Press Ctrl-F9 to execute, and on the right side, you can see that the file is read in the form of a data table:

Write in A2:
$select * from d:/Orders.txt
And press Alt-Enter to execute the cell, esProc IDE allows for single step execution of a certain statement, which is particularly convenient for interactive data analysis. You will see the same result on the right:
The SQL of esProc considers txt to be TAB separated and csv to be comma separated, and will automatically process them based on the extension. If the extension is written incorrectly, the result may be read incorrectly. Some files do not have a title line:

$select * from {file("d:/OrdersNT.csv").import@c()}
In the code, {...} is SPL native syntax, which means reading a text file without a title line and separated by commas. After execution, the results can be seen:

Then, we can try calculating. First, look at WHERE and write in A4:
$select * from d:/Orders.csv where Amount>=100 and Client like '%s%'
The result after execution:

A5:$select * from d:/Orders.csv where (OrderDate<date('2020-01-01') and Amount<=100) or (OrderDate>=date('2020-12-31') and Amount>100)

A6: $select case year(OrderDate) when 2010 then 'this year' when 2010 then 'last year' else 'previous years' end from d:/Orders.csv

A7:$select Client,year(OrderDate) y,sum(Amount) amt from d:/Orders.csv group by Client,year(OrderDate)

A8:$select Client,year(OrderDate) y,sum(Amount) amt from d:/Orders.csv
group by Client,year(OrderDate) having sum(Amount)>1000

A9: $select o.OrderId,o.Client,e.Name e.Dept,e.EId from d:/Orders.csv o
left join d:/Emp.csv e on o.SellerId=e.Eid


SPL Resource: SPL Official Website | SPL Blog | Download esProc SPL | SPL Source Code

