问题
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