Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to optimize following three queries into a single query.
SQL
select sum(M.amount) into var1 from t_bill_item M where M.tax =0;
select sum(M.amount) into var2 from t_bill_item M where M.tax =4;
select sum(M.amount) into var3 from t_bill_item M where M.tax =13.5;


var1,var2 and var3 are mysql variables.
Posted
Updated 6-Nov-14 0:03am
v4
Comments
Tomas Takac 6-Nov-14 6:00am    
What's the problem with having three separate queries?
Himu from Orissa 6-Nov-14 6:43am    
Actually these queries are written in a function which is used in a query. So its taking some long time.

You may try this, But would have to check the performance difference between two as data is with you.
SQL
select
sum(Case when M.tax= 0 then M.amount else 0 end) As var1
,sum(Case when M.tax= 4 then M.amount else 0 end) As var2
,sum(Case when M.tax= 13.5 then M.amount else 0 end) As var3
from t_bill_item M where M.tax in (0,4,13.5);
 
Share this answer
 
v2
Comments
Himu from Orissa 6-Nov-14 9:48am    
seems the one was looking for. Will test it
Shweta N Mishra 7-Nov-14 3:32am    
:) Please mark the answer as accepted.
Here is the example

SQL
select 1 as var1
select 2 as var1
select 3 as var1

into
SQL
select *from (select 1 as var1 )t1,(select 2 as var2)t2,(select 3 as var3 )t3



Modify the Query :)
 
Share this answer
 
Comments
Himu from Orissa 6-Nov-14 6:44am    
Here also you are using three selects. Think it wont take less time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900