I am trying to improve query performance, the query structure is as follows:
select 'Total_amount', (select SUM(Total) from dbo.Invoices i where i.BU = bun.BU), case when (select SUM(Total) from dbo.Invoices i where i.BU = bun.BU) > 100000 then 'Good' else 'Not good' end from dbo.BusinessUnits bun
I know that this example can be solved with the help of unions, but for my real requests I need subqueries. As you can see, I have the same subquery twice, one to give the actual value, and the other to calculate the state.
Is there a way to improve performance by simply calculating the subquery once?
sql sql-server
Daniel Martinez
source share