-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcFinancialRatio.sql
31 lines (30 loc) · 1.07 KB
/
calcFinancialRatio.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- stored procedure to calculate financial ratios and store them in the table outputTable
USE [Zenith]
GO
/****** Object: StoredProcedure [dbo].[updateOutputTable] Script Date: 30/09/2022 6:54:13 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE proc [dbo].[calcFinancialRatio]
@ratioName varchar(20),
@ratioNumerator varchar(30),
@ratioDenominator varchar(30)
as
begin
declare @strQuery as varchar(max)
set @strQuery =
'with cte1 (StockId, ReportMonth, ReportYear, FinRatio) as ' +
'(' +
'select a.StockID, a.ReportMonth, a.ReportYear, cast(a.ItemValue/b.ItemValue as float) ' +
'from Financials a inner join Financials b ' +
'on a.StockID=b.STockID and a.ReportMonth=b.ReportMonth and a.ReportYear=b.ReportYear ' +
'where b.ItemValue<>0 ' +
'and a.Item=''' + @ratioNumerator + '''' +
'and b.Item=''' + @ratioDenominator + '''' +
')' +
'update a set a.'+@ratioName+'=b.FinRatio ' +
'from outputTable a inner join cte1 b ' +
'on a.StockID=b.StockID and a.ReportMonth=b.ReportMonth and a.ReportYear=b.ReportYear'
exec(@strQuery)
end