use market_data;


select * from zacksrank where symbol in ('LIVN','MRCY','LOPE','ASIX','HLF')


--AnalystRatingsDA.GetAnalystRatings(selectedSymbol);
--This allows a ZacksRank we received up to 30 days ago to apply to an particular analyst ratings date.
select distinct ar.date,ar.symbol,ar.company,ar.brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar 
left join zacksrank zr on ar.symbol=zr.symbol and (zr.date <= ar.date and zr.date > date_add(ar.date ,interval -30 day))
where ar.symbol='AMAT'


-- AnalystRatingsDA.GetAnalystRatings(selectedSymbol, DateTime.Parse(selectedDate));
select ar.date,ar.symbol,ar.company,brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar
left join zacksrank zr on ar.symbol=zr.symbol and ar.date=zr.date 
where ar.date='2018-05-14' and ar.symbol='AMAT' order by symbol


--AnalystRatingsDA.GetAnalystRatings(DateTime.Parse(selectedDate));
select ar.date,ar.symbol,ar.company,ar.brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar 
left join zacksrank zr on ar.symbol=zr.symbol and ar.date=zr.date
where ar.date='2018-05-18'
order by ar.symbol


--AnalystRatingsDA.GetAnalystRatingsMaxDate(String symbol,DateTime maxDate)
select ar.date,ar.symbol,ar.company,ar.brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar
left join zacksrank zr on ar.symbol=zr.symbol and ar.date=zr.date
where ar.symbol='AMAT' and 
ar.date<='2018-05-19'
order by ar.date

--AnalystRatingsDA.AnalystRatings GetAnalystRatings(String symbol, DateTime minDate,DateTime maxDate)
select ar.date,ar.symbol,ar.company,ar.brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar
left join zacksrank zr on ar.symbol=zr.symbol and ar.date=zr.date
where ar.symbol='AMAT' and 
ar.date>='2018-05-01' and
ar.date<='2018-05-19'
order by ar.date


--AnalystRatingsDA.AnalystRatings GetAnalystRatings()
select distinct ar.date,ar.symbol,ar.company,ar.brokerage_firm,ar.type,ar.ratings_change,ar.price_target,zr.zacks_rank from analystratings ar
left join zacksrank zr on ar.symbol=zr.symbol and (zr.date <= ar.date and zr.date > date_add(ar.date ,interval -30 day))
where zr.zacks_rank is not null
order by ar.date desc

