|
puvpul
Starting Member
6 Posts |
Posted - 2011-10-11 : 07:15:07
|
| Dear Experts,I’m trying to create view/stored procedure that would subtract current quarter value to the previous quarter value. Few of the data for current quarter value are not available. In this case it will subtract previous quarter to the previous one. example are shown below: CREATE TABLE profitq ( ticker varchar(16) NOT NULL, year int NOT NULL, pq1 decimal(12,2) DEFAULT NULL, pq2 decimal(12,2) DEFAULT NULL, pq3 decimal(12,2) DEFAULT NULL, pq4 decimal(12,2) DEFAULT NULL, PRIMARY KEY (ticker,year))goINSERT profitq VALUES('ABBANK', 2010, '20', '25', null, null),('ALARA', 2010, '10', null, null, null),('BASIA', 2010, '20', null, null, null)goCREATE TABLE profitq10 ( ticker varchar(16) NOT NULL, year int NOT NULL, pq1 decimal(12,2) DEFAULT NULL, pq2 decimal(12,2) DEFAULT NULL, pq3 decimal(12,2) DEFAULT NULL, pq4 decimal(12,2) DEFAULT NULL, PRIMARY KEY (ticker,year))goINSERT profitq10 VALUES('ABBANK', 2010, '20', '25', '10', '15'),('ALARA', 2010, '10', '15', '12', '6'),('BASIA', 2010, '20', '30', '40', '50')The view I’ve created is following:select ticker, ((pq2/pq1)-1) as quartergrowthfrom profitqcurrent quarterly data for few companies is not available yet. In this case I need to perform the calculation like select a.ticker, ((a.pq1/b.pq4)-1) as qurtergrowthFrom profitq aJoin profitq10 b On a.ticker=b.ticker;[formula-- (currentquarteramount/previousquarteramount)-1] Can you please help me on this. i'm using sql server 2008. thx in advanceRegardsWakil |
|