Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Coders,

I have a following query:

I want to take Stock price avg for last 3 days,10 days etc.There are thousand of stocks.

The dates are not in order i.e. Monday to Friday only as market not open on saturday & Sunday but sometime there is a holiday in between market days i.e. can say Wednesday is a holiday.Sometime market open on Saturday also.


This is the data for dates:

Tradedate:
VB
2012-12-17
2012-12-14
2012-12-13
2012-12-12
2012-12-11
2012-12-10
2012-12-07
2012-12-06
2012-12-05
2012-12-04
2012-12-03
2012-11-30
2012-11-29
2012-11-27
2012-11-26
2012-11-23
2012-11-22
2012-11-21
2012-11-20
2012-11-19
2012-11-16


These are the trade date & each tradedate has thousands of stock names along with their price & traded volumes.

I want to take avg volumes of last three trading days but something is missing when i tried that so requesting all of you to help me.

here i want the avg volumes for last 3 trade date.The dates mentioned here are in descending order.

Hope i will get some query to do this.

Thanks & Regards.
Posted

HI,

For getting the last N number of trading date you can view the following link.

sql query to get the last N trading days

This may show you the right way.

Thanks
 
Share this answer
 
Comments
[no name] 17-Dec-12 23:30pm    
If you think this helped you then please upvote it to 5 stars...
that will be considered as Upvoted... Otherwise it will be considered as down voted...
Please change.
Here is the sql for it.

SQL
select pricesymbol,avg(volume) as averagevolume,avg(price) as averageprice from YOURTABLE group by pricesymbol;
 
Share this answer
 
Dear All,

Thanks For the response especially from Armando Talex. I tested the solution suggested by him; it's working fine.

But i found my own solution to solve this as follows:

--First drop the table.As i have created a procedure to do this calculation i am droping the table which is crated for temp purpose

drop table #date

--The below query will give me top 8 trading dates which are in the database & these dates will get store in a temp table #date

select top 8 tradedate into #date from EODNSE group by TradeDate order by TradeDate desc

--The below query will give me the enddate i.e. 8th trade date
select top 1 tradedate from #date order by tradedate

Following is the full queries to do this:

SQL
drop table #date
select top 8 tradedate into #date from EODNSE group by TradeDate order by TradeDate desc
declare @StartDate date --@Startdate is the current date of today
declare @Enddate date --@Enddate is the current date less 2 day's back date
set @StartDate = (select top 1 TradeDate from EODNSE order by TradeDate desc) --This will get the current date
set @Enddate = (select top 1 tradedate from #date order by tradedate)--This will take the last date from #date temp table
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900