# question

## need help for dynamic getting data

i have a query like MAX(CASE WHEN FY = 'FY10' THEN PurAmt END) AS FY10_PurAmt, MAX(CASE WHEN FY = 'FY11' THEN PurAmt END) AS FY11_PurAmt, MAX(CASE WHEN FY = 'FY12' THEN PurAmt END) AS FY12_PurAmt, MAX(CASE WHEN FY = 'FY13' THEN PurAmt END) AS FY13_PurAmt, MAX(CASE WHEN FY = 'FY14' THEN PurAmt END) AS FY14_PurAmt, MAX(CASE WHEN FY = 'FY15' THEN PurAmt END) AS FY15_PurAmt So I need query like dynamically get the data for each FY years I needed to change FY11 as dynamic and so on

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Can you elaborate what do you want to achieve please?
0 Likes 0 ·
actually I need to get every FiscalYear's PurchaseAmount data. For this we done in this code like static way by giving FY09,FY10..... but what I need to get PurchaseAmount data dynamically based on FiscalYear
0 Likes 0 ·
0 Likes 0 ·

·
----FIRST CREATE A FUNCTION THAT RETURNS YOU DATA Create function udf_getpamount(@inp varchar(10)) returns int begin declare @var int select @var=PurAmt from your_table where FY=@inp return @var end ---SECOND CALL THIS FUNCTION select udf_getpamount('FY10')

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ADD BELOW LOGIC AS WELL:- DECLARE @FINAL_STRING VARCHAR(1000) declare @var2 VARCHAR(100)='FY10' while financial_year_count >0 begin SELECT @FINAL_STRING = @FINAL_STRING + ','+ udf_getpamount(@var2) DECLARE @YR INT =RIGHT(@VAR2,'2') SET @YR=@YR-1 SET @VAR2=LEFT(@VAR2,'2')+@YR SET financial_year_count = financial_year_count -1 end

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.