sect<-c("Healthcare","Basic Materials","Utilities","Financial Services","Technology","Consumer"
"Defensive","Industrials","Communication Services","Energy","Real Estate","Consumer
Cyclical","NULL")
mcap<-c("3 - Large","2 - Mid","1 - Small")
df_total = data.frame()
start <- as.Date("01-01-14",format="%d-%m-%y")
end <- as.Date("18-03-20",format="%d-%m-%y")
theDate <- start
while (theDate <= end){
for (value1 in sect){
for (value2 in mcap){
date=theDate
sector<-value1
marketcap1<-value2
newquery("Select * from table where date='%s' and sector='%s' and
marketcap='%s'",date,sector,marketcap1)
topdemo <- sqlQuery(dbhandle,newquery)
df=data.frame(topdemo)
df_total <- rbind(df_total,df)
}
}
theDate <- theDate + 1
}
How to loop this code in SQL Server so that the execution time is not too long and append it to the same select statement? I need to loop the query in SQL so that it goes through every date, market cap, and sector and compute certain things. At last, the appended query will be written in the database.
Note: the Select
query shown above is just a sample query. In my work, I'm doing a lot of computations using SQL.
Note: I cant use 'between' or 'In' command because in my computation Im taking an average of a column on that particular date, sector, and market cap. If I use 'between' for dates it is going to take an average of all the dates given.