Analytic function and select specific row

独自空忆成欢 提交于 2019-12-08 11:26:06

问题


I have a table

Tdate        Name      value
20130101     xxx        1.2
20130102     xxx        1.4
...
20130101     yyy        1.3
20130102     yyy        2.3

I want to calculate the value correlation between each name in Jan 2013, and list top 20 of them. Here is my code:

With targetRow as (
Select  XXX
Where tDate is “20130131”)

Select  a.name, b.name, corr(a.value,b.value) over (partition by name
Range  between 31  preceding and targetRow)  Correlation
From logprofittest a, logprofittest b
Order by correlation

Here are questions:

(1) How to complete my with clause (or there is any better way). My Tdate is stored as Integer?)

(2) How to list only top 100 if my table is too large that joining itself is too expensive?

Update: sorry for the confusion, I fix the code a bit.

Select  a.Symbol, b.Symbol, corr(a.logp,b.logp) over (partition by a.symbol,b.symbol)  Correlation
From logprofittest a, logprofittest b
where a.Symbol<>b.symbol
And  To_date(a.Tdate,'YYYYMMDD') between  DATE'2013-12-01' AND DATE'2013-12-31'
and  To_date(b.Tdate,'YYYYMMDD') between  DATE'2013-12-01' AND DATE'2013-12-31'
and  To_date(a.Tdate,'YYYYMMDD')=To_date(b.Tdate,'YYYYMMDD')
Order by correlation desc;

来源:https://stackoverflow.com/questions/28748431/analytic-function-and-select-specific-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!