Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jcarrallo
Starting Member
35 Posts |
Posted - 2014-09-12 : 14:15:25
|
Hello,Thanks for helping.I have a select statement that works fine. Now I need to create a view with this statement and I have problems with the variables that I need to declare.Can anyone help? Is there a way to include the declare command in a view?I can not use a stored procedure this time.Many thanks,JayThis is the statement:_____________________________________DECLARE @FirstDate DATE, @SecondDate DATE, @ThirdDate DATESELECT @FirstDate = MAX(fecha_valor) FROM MPR_Historico_Posiciones---SELECT @SecondDate = MAX(fecha_valor) FROM MPR_Historico_Posiciones WHERE fecha_valor<> @FirstDateSELECT @SecondDate = dateadd(week,-1,max(fecha_valor)) FROM MPR_Historico_Posiciones---SELECT @ThirdDate = dateadd(week,-2,max(fecha_valor)) FROM MPR_Historico_PosicionesSELECT description, CAST(SUM(Case fecha_valor when @FirstDate THEN valor_div_global ELSE 0 END) AS numeric(12, 3)) as col_1_Valor, CAST(SUM(Case fecha_valor when @FirstDate THEN mar_price ELSE 0 END) AS numeric(12, 3)) as col_1_Precio, CAST(SUM(Case fecha_valor when @FirstDate THEN plusvalia_global ELSE 0 END) AS numeric(12, 3)) as col_1_Result, CAST(SUM(Case fecha_valor when @SecondDate THEN valor_div_global ELSE 0 END) AS numeric(12, 3)) as col_2_Valor, CAST(SUM(Case fecha_valor when @SecondDate THEN mar_price ELSE 0 END) AS numeric(12, 3)) as col_2_Precio, CAST(SUM(Case fecha_valor when @SecondDate THEN plusvalia_global ELSE 0 END) AS numeric(12, 3)) as col_2_ResultFROM MPR_Historico_PosicionesGROUP BY description,symbol; |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-09-12 : 14:24:08
|
You can't use variables in a view. Why does it need to be a view?Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
jcarrallo
Starting Member
35 Posts |
Posted - 2014-09-12 : 15:36:16
|
I see.I need a view because I use alpha five for the front end to generate a grid, and with this type of statement Alpha Five would not be able to identify the various columns resulting from the statement. |
|
|
|
|
|
|
|