sql server - Mixing date frequencies in SQL -


i have query below:

select s1.datadate, s1.prccd, c.ebit sec_dprc s1  left outer join rdq_temp c  on s1.gvkey = c.gvkey , s1.datadate = c.rdq s1.gvkey = 008068 order s1.datadate 

i trying create rolling calculation between 2 columns, prccd column daily prices , ebit column quarterly value. want able calculate product of two, i.e prccd*ebit everyday ebit changes once quarter on random dates. summarizing, want able calculating product of ebit , prccd going forward using new values of ebit when change each quarter randomly

datadate                prccd   ebit 1984-02-01 00:00:00.000 28.625  null 1984-02-02 00:00:00.000 27.875  null 1984-02-03 00:00:00.000 26.75   420.155 1984-02-06 00:00:00.000 27      null 1984-02-07 00:00:00.000 26.875  null 

. . .

datadate                prccd   ebit 1984-05-02 00:00:00.000 30.75   null 1984-05-03 00:00:00.000 30.875  null 1984-05-04 00:00:00.000 30.75   null 1984-05-07 00:00:00.000 31.125  499.228 1984-05-08 00:00:00.000 31.75   null 

. . .

1984-07-31 00:00:00.000 25.625  null 1984-08-01 00:00:00.000 26.75   null 1984-08-02 00:00:00.000 26.375  348.364 1984-08-03 00:00:00.000 26.75   null 1984-08-06 00:00:00.000 27      null 

thanks help!

one of solutions came to:

select td.date, td.c cd, tq.c cq, tq.c1, tq.c/tq.c1 ebitps,tq.c/tq.c1/td.c pe  (select datadate date, prccd c sec_dprc gvkey = 008068) td cross apply (select top 1 rdq date, ebit c, csh12q c1 rdq_temp  rdq<=td.date order rdq desc) tq order td.date 

what looking non-equijoin between 2 tables. easier if had effective , end date on rdq_temp data. in order add them in sql server, can self join , aggregation (other databases support lag() , lead() functionality).

the following query condition on join "between":

with rdq (     select r.datadate, r.ebit, min(rnext.datadate) nextdatadate     rdq_temp r left outer join          rdq_temp rnext          on r.datadate < rnext.datedate     group r.datadate, r.ebit ) select  datadate, prccid, rdq.ebit sec_dprc sd left outer join      rdq      on sd.datadate >= rdq.datadate , rdq.datadate < rdq.nextdatadate 

i'm guessing data quarters not big, should work fine. if had more data, suggest having effective , end dates, rather asof date, in rdq records.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -