views:

122

answers:

1

Hi,

Consider the following data:

Insurance_Comp | 1To30DaysAgeing | 31To60DaysAgeing | 61To90DaysAgeing | TotalVehicles

=============================================================================

ABC | 30 | 5 | 20 | 55

XYZ | 10 | 35 | 5 | 50

I am calculating the number of vehicles aged for particular group after a stock# is assigned to that vehicle. The number of vehicles for a group (ex. 1 to 30 Days Ageing) is calculated using a complex query. I have written SP to get this result. What I want is to calculate the total of vehicles while executing the same select query. For simplification I have created functions in SQL to get number of vehicles for each group.

Right now I m using the query like this ...

Select Ins_Comp, dbo.fn_1To30Ageing(...), dbo.fn_31To60Ageing(...), dbo.fn_61To90Ageing(...) from Table Where ....

I am calculating the total using RowDataBound event of GridView in ASP.NET with C#. Is there any way to calculate the total within query itself? By the way I dont want total as dbo.fn_1To30Ageing(...)+ dbo.fn_31To60Ageing(...) + dbo.fn_61To90Ageing, because that requires double processing time.

Thanks for sharing your valuable time...

+1  A: 

I'm not familir with ASP.NET, but if it is only SQL aspect, you can certainly use sub-query:

SELECT A,B,C,SUM(A+B+C) FROM ( Select Ins_Comp, dbo.fn_1To30Ageing(...) AS A, dbo.fn_31To60Ageing(...) AS B, dbo.fn_61To90Ageing(...) AS C from Table Where .... ) TEMP

bryantsai
I think you have not read my question completely. The values of A,B,C are calculated through functions and I dont want total again by calling them.
IrfanRaza
Hey, easy there, your question is confusing due to lack of info. The above query only uses each function once, where is the problem?
RedFilter
Yes, the sub-query one only execute those 3 functions once, not twice.
bryantsai
Yeah, You are correct.
IrfanRaza
I need to nest select while i was trying single select.
IrfanRaza