Search code examples
azuret-sqlazure-stream-analytics

How to calculate percentage for each row using azure stream analytics?


I'm getting data in real-time from NSE(national stock exchange) through the event hub to stream analytics like the following format:

 [{symbol: 'HCLTECH',
   openPrice: '1,097.00',
   highPrice: '1,125.00',
   lowPrice: '1,092.30',
   ltp: '1,122.80'},
 { symbol: 'BPCL',
   openPrice: '342.00',
   highPrice: '351.45',
   lowPrice: '337.50',
   ltp: '350.45'
 }]

as you can in json that, open price - starting price for today, ltp - current price if open price is 100 and ltp is 150 then change is 50% how can I do this for all rows, using azure stream analytics.


Solution

  • Division feature is not found in the Mathematical Functions,so you have to calculate it by yourself with UDF in ASA.

    UDF:

    function main(arg1, arg2) {
        var f = parseFloat((arg2-arg1)/arg1);    
        if (isNaN(f)) {   
         return '0%';    
        }          
        f = Math.round(f*100)/100;  
        return f*100+"%";        
    }
    

    SQL:

    SELECT    
        udf.divCal(input.openPrice,input.ltp)
    FROM input