开发者

How do I use SQL table alias for more SELECT and with different condition?

开发者 https://www.devze.com 2023-04-07 02:43 出处:网络
I want to generate ssrs report for that i need to select 4 values PreviousNAV , TodaysNAV , @NetChange , @BAMLIndex

I want to generate ssrs report for that i need to select 4 values PreviousNAV , TodaysNAV , @NetChange , @BAMLIndex

CREATE PROCEDURE [dbo].[Progreereport]
(
@EndDate DATETIME,
@ComparePeriod VARCHAR(10),
@PortfolioId VARCHAR(50) = '6'
)
AS
BEGIN

DECLARE @StartDate DATETIME =
    CASE
        WHEN @ComparePeriod = 'Daily' THEN dbo.DateUtility_MakeBusinessDay(DATEADD(DAY, -1, @EndDate), 'LessThanOrEqualTo')
        WHEN @ComparePeriod = 'Weekly' THEN dbo.DateUtility_GetRelativeDate(@EndDate, 'LessThan', 'End', 'Week', 1)
        WHEN @ComparePeriod = 'MTD' THEN dbo.DateUtility_GetRelativeDate(@EndDate, 'LessThan', 'End', 'Month', 1)
        WHEN @ComparePeriod = 'QTD' THEN dbo.DateUtility_GetRelativeDate(@EndDate, 'LessThan', 'End', 'Quarter', 1)
        WHEN @Compa开发者_C百科rePeriod = 'YTD' THEN dbo.DateUtility_GetRelativeDate(@EndDate, 'LessThan', 'End', 'Year', 1)
        WHEN @ComparePeriod = 'LTM' THEN DATEADD(MONTH, -12, @EndDate)
        WHEN @ComparePeriod = 'LTD' THEN CAST(Fireball_Configuration.dbo.GetConfigurationValue('Fireball', 'Concerto Credit Opportunity Master Fund I - Inception Date') AS DATETIME)
    END

  DECLARE @PreviousNAV DECIMAL(18,4)
  DECLARE @TodaysNAV DECIMAL(18,4)
  DECLARE @NetChange DECIMAL(18,4)
  DECLARE @BAMLIndex DECIMAL(18,4)

  SELECT ISNULL(@PreviousNAV,0) PreviousNAV 
  FROM Fireball..NAV
  WHERE Date = @StartDate and PortfolioId = @PortfolioId 

  SELECT ISNULL(@TodaysNAV,0) TodaysNAV 
  FROM Fireball..NAV
  WHERE Date = @EndDate and PortfolioId = @PortfolioId 

  SELECT @NetChange = (@TodaysNAV - @PreviousNAV ) 

  SELECT @BAMLIndex  = @NetChange / @TodaysNAV

  END
  GO

I need above four values to generate report but help me to put it into table alias so that i can use it very easily...


Do you mean you want to return the items in a table?

Do this after your current script:

DECLARE @myData TABLE (
   PreviousNAV DECIMAL(18,4),
   TodaysNAV DECIMAL(18,4),
   NetChange DECIMAL(18,4), 
   BAMLIndex DECIMAL(18,4) 
)

INSERT @myData
VALUES (@PreviousNAV, @TodaysNav, @NetChange, @BAMLIndex)

SELECT * FROM @myData

To return a single column of the four values, add this at the end of your stored procedure:

  SELECT @PreviousNAV as [Data] UNION
  DECLARE @TodaysNAV UNION
  DECLARE @NetChange UNION
  DECLARE @BAMLIndex 
0

精彩评论

暂无评论...
验证码 换一张
取 消