Created
January 16, 2020 06:28
-
-
Save tallpeak/aa16ccd1f66b404d37c40cddb7ff1d7e to your computer and use it in GitHub Desktop.
export some data from SQL to XLSX using Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DataFrames, XLSX | |
getsqlpass() = string(strip( open(h->read(h,String), "/home/aaron/secrets/sqlpassword") ,['\r','\n',' ','\t'])) | |
import ODBC | |
coals = [("CIMS",1016),("AIMS",1019)] | |
asv = ["A","v"] | |
yr = 20 | |
for (coalName,coalNum) in coals | |
for aors in asv | |
tn = string("top_", coalName, "_sales_", aors) | |
sql = string("exec sp_getsales_a4b | |
'01/01/",yr-1,"','01/01/",yr,"', | |
'01/01/",yr-2,"','01/01/",yr-1,"', | |
'01/01/",yr-3,"','01/01/",yr-2,"', | |
'01/01/",yr-4,"','01/01/",yr-3,"', | |
@sort='rk',@genre=0,@km='All ',@aors='", aors, "',@dma=0,@coal=",coalNum,", | |
@chain=0,@client=0,@media_format=0,@ind=-1,@source=7, | |
@label='',@userid=0,@dist='',@chainex=0,@topnum='10000'") | |
println(sql) | |
dsn = ODBC.DSN("quadvm", "SA", getsqlpass()) | |
ODBC.execute!(dsn, "use streetpulse") | |
df = ODBC.query(dsn, sql) | |
for i = 1:size(df,2) | |
if eltype(df[:,i]) <: Union{Missing, Int32} | |
println("converting column ",i) | |
#df[:,i] *= 1.0 | |
df[:,i] = convert.(Union{Missing, Int64}, df[:, i]) | |
end | |
end | |
for i = 1:size(df,2) | |
println("col",i,"=", eltype(df[:,i]) ) | |
end | |
fnam = tn * ".xlsx" | |
rm(fnam, force=true) | |
XLSX.writetable(fnam, collect(DataFrames.eachcol(df)), DataFrames.names(df)) | |
ODBC.disconnect!(dsn) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment