query+='COUNT(CASE WHEN (fiscal_year = "'+from_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_1.split('~~~')[0]+'" AS DATE) AND CAST("'+date_1.split('~~~')[1]+'" AS DATE))) THEN name ELSE NULL END),SUM(CASE WHEN (fiscal_year = "'+from_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_1.split('~~~')[0]+'" AS DATE) AND CAST("'+date_1.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MIN(CASE WHEN (fiscal_year = "'+from_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_1.split('~~~')[0]+'" AS DATE) AND CAST("'+date_1.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MAX(CASE WHEN (fiscal_year = "'+from_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_1.split('~~~')[0]+'" AS DATE) AND CAST("'+date_1.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),AVG(CASE WHEN (fiscal_year = "'+from_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_1.split('~~~')[0]+'" AS DATE) AND CAST("'+date_1.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),'
else:
query+='COUNT(CASE WHEN fiscal_year = "'+from_fiscal_year+'" THEN name ELSE NULL END),SUM(CASE WHEN fiscal_year = "'+from_fiscal_year+'" THEN net_total ELSE NULL END),MIN(CASE WHEN fiscal_year = "'+from_fiscal_year+'" THEN net_total ELSE NULL END),MAX(CASE WHEN fiscal_year = "'+from_fiscal_year+'" THEN net_total ELSE NULL END),AVG(CASE WHEN fiscal_year = "'+from_fiscal_year+'" THEN net_total ELSE NULL END),'
query+='COUNT(CASE WHEN (fiscal_year = "'+f_year+'" and (transaction_date BETWEEN CAST("'+date_2.split('~~~')[0]+'" AS DATE) AND CAST("'+date_2.split('~~~')[1]+'" AS DATE))) THEN name ELSE NULL END),SUM(CASE WHEN (fiscal_year = "'+f_year+'" and (transaction_date BETWEEN CAST("'+date_2.split('~~~')[0]+'" AS DATE) AND CAST("'+date_2.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MIN(CASE WHEN (fiscal_year = "'+f_year+'" and (transaction_date BETWEEN CAST("'+date_2.split('~~~')[0]+'" AS DATE) AND CAST("'+date_2.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MAX(CASE WHEN (fiscal_year = "'+f_year+'" and (transaction_date BETWEEN CAST("'+date_2.split('~~~')[0]+'" AS DATE) AND CAST("'+date_2.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),AVG(CASE WHEN (fiscal_year = "'+f_year+'" and (transaction_date BETWEEN CAST("'+date_2.split('~~~')[0]+'" AS DATE) AND CAST("'+date_2.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),'
else:
query+='COUNT(CASE WHEN fiscal_year = "'+f_year+'" THEN name ELSE NULL END),SUM(CASE WHEN fiscal_year = "'+f_year+'" THEN net_total ELSE NULL END),MIN(CASE WHEN fiscal_year = "'+f_year+'" THEN net_total ELSE NULL END),MAX(CASE WHEN fiscal_year = "'+f_year+'" THEN net_total ELSE NULL END),AVG(CASE WHEN fiscal_year = "'+f_year+'" THEN net_total ELSE NULL END),'
query+='COUNT(CASE WHEN (fiscal_year = "'+to_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_3.split('~~~')[0]+'" AS DATE) AND CAST("'+date_3.split('~~~')[1]+'" AS DATE))) THEN name ELSE NULL END),SUM(CASE WHEN (fiscal_year = "'+to_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_3.split('~~~')[0]+'" AS DATE) AND CAST("'+date_3.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MIN(CASE WHEN (fiscal_year = "'+to_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_3.split('~~~')[0]+'" AS DATE) AND CAST("'+date_3.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),MAX(CASE WHEN (fiscal_year = "'+to_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_3.split('~~~')[0]+'" AS DATE) AND CAST("'+date_3.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END),AVG(CASE WHEN (fiscal_year = "'+to_fiscal_year+'" and (transaction_date BETWEEN CAST("'+date_3.split('~~~')[0]+'" AS DATE) AND CAST("'+date_3.split('~~~')[1]+'" AS DATE))) THEN net_total ELSE NULL END)'
else:
query+='COUNT(CASE WHEN fiscal_year = "'+to_fiscal_year+'" THEN name ELSE NULL END),SUM(CASE WHEN fiscal_year = "'+to_fiscal_year+'" THEN net_total ELSE NULL END),MIN(CASE WHEN fiscal_year = "'+to_fiscal_year+'" THEN net_total ELSE NULL END),MAX(CASE WHEN fiscal_year = "'+to_fiscal_year+'" THEN net_total ELSE NULL END),AVG(CASE WHEN fiscal_year = "'+to_fiscal_year+'" THEN net_total ELSE NULL END)'
list_range+=1
# Main Query
det=sql("SELECT %s from `tab%s` where %s"%(query,r[col_idx['ID']],condition))