Thread View: borland.public.interbase.sql
8 messages
8 total messages
Started by Isaac
Tue, 27 May 2008 08:46
can I do that with one select statement
Author: Isaac
Date: Tue, 27 May 2008 08:46
Date: Tue, 27 May 2008 08:46
19 lines
1249 bytes
1249 bytes
Hello, I need to to sum a field and store the result into a variable in a stored procedure, can I use one select statement to get the sum based on a condition from another field ex sum(field_a) when field_b = 1 sum(field_a) when field_b = 0 field_a field_b 5 1 10 0 52 1 or I have to use two select ? sorry this may appear stupid but I am asking to improve performance Thanks
Re: can I do that with one select statement
Author: Maxim Shiryaev
Date: Tue, 27 May 2008 12:01
Date: Tue, 27 May 2008 12:01
40 lines
1801 bytes
1801 bytes
Hi. Try: for select field_a, sum(field_b) from t group by field_a into :v_a, :v_sum_b do begin if (v_a) = 1 then some_var_1 = v_sum_b; else if (v_a) = 2 then some_var_2 = v_sum_b; /* and more if's if ther are more distinct values in field_a */ end Maxim. "Isaac" <no@no.com> ???????/???????? ? ???????? ?????????: news:483bca8e@newsgroups.borland.com... > Hello, > I need to to sum a field and store the result into a variable in a > stored procedure, can I use one select statement to get the sum based on > a condition from another field > > ex > sum(field_a) when field_b = 1 > sum(field_a) when field_b = 0 > > field_a field_b > 5 1 > 10 0 > 52 1 > > > or I have to use two select ? sorry this may appear stupid but I am > asking to improve performance > > Thanks
Re: can I do that with one select statement
Author: Isaac
Date: Tue, 27 May 2008 13:54
Date: Tue, 27 May 2008 13:54
21 lines
1489 bytes
1489 bytes
Maxim Shiryaev wrote: > Hi. > > Try: > > for > select field_a, sum(field_b) > from t > group by field_a > into :v_a, :v_sum_b > do begin > if (v_a) = 1 then some_var_1 = v_sum_b; > else if (v_a) = 2 then some_var_2 = v_sum_b; > /* and more if's if ther are more distinct values in field_a */ > end Is this faster than using Two Selects Select sum(f2) from table_1 where f1 = 1 Select sum(f2) from table_2 where f1 = 0 Thanks
Re: can I do that with one select statement
Author: Isaac
Date: Tue, 27 May 2008 19:34
Date: Tue, 27 May 2008 19:34
3 lines
1057 bytes
1057 bytes
Please Mr Maxim Shiryaev confirm if this method is faster than 2 Selects Thanks
Re: can I do that with one select statement
Author: Wayne Niddery (TeamB)
Date: Wed, 28 May 2008 02:42
Date: Wed, 28 May 2008 02:42
12 lines
1692 bytes
1692 bytes
"Isaac" <no@no.com> wrote in message news:483c6256@newsgroups.borland.com... > Please Mr Maxim Shiryaev confirm if this method is faster than 2 Selects Should be pretty quick to test. Try it. Often there is not way to say positively which is faster, it depends on your tables, anount of data, indexes, etc. -- Wayne Niddery - TeamB (www.teamb.com) Winwright, Inc. (www.winwright.ca)
Re: can I do that with one select statement
Author: Isaac
Date: Wed, 28 May 2008 10:08
Date: Wed, 28 May 2008 10:08
10 lines
1350 bytes
1350 bytes
Wayne Niddery (TeamB) wrote: > Should be pretty quick to test. Try it. > > Often there is not way to say positively which is faster, it depends on > your tables, anount of data, indexes, etc. > I dont guarantee that my test will accurate to determine however I will use for select Thanks
Re: can I do that with one select statement
Author: Maxim Shiryaev
Date: Thu, 29 May 2008 14:08
Date: Thu, 29 May 2008 14:08
60 lines
2452 bytes
2452 bytes
I cannot confirm that it will be faster in all cases. But _generally_, "sequential scan and in-memory sorting is faster than several index scans". (C) Someone from FB dev team. And I can confirm that in most cases it's so. Especially if your indicies are not very selective. So it all depends. Probably the ultimate way would be: declare variable v1 integer; declare variable v2 integer; v1 = 0; v2 = 0; select field_a, field_b from t into :v_a, :v_b do begin if (v_a = 1) then v1 = v1 + v_b; else if (v_a = 2) then v2 = v2 + v_b; /* and more if's if ther are more distinct values in field_a */ end But here you again trade code clarity and "declarativeness" for performance. Just test it. But if there are many different values in field_a and you count only sums for subset of them than several indexed scans would be faster of course. Maxim. "Isaac" <no@no.com> ???????/???????? ? ???????? ?????????: news:483c12b2@newsgroups.borland.com... > Maxim Shiryaev wrote: > > Hi. > > > > Try: > > > > for > > select field_a, sum(field_b) > > from t > > group by field_a > > into :v_a, :v_sum_b > > do begin > > if (v_a) = 1 then some_var_1 = v_sum_b; > > else if (v_a) = 2 then some_var_2 = v_sum_b; > > /* and more if's if ther are more distinct values in field_a */ > > end > Is this faster than using Two Selects > > Select sum(f2) from table_1 where f1 = 1 > Select sum(f2) from table_2 where f1 = 0 > > Thanks
Thanks Maxim
Author: Isaac
Date: Fri, 30 May 2008 19:05
Date: Fri, 30 May 2008 19:05
5 lines
1134 bytes
1134 bytes
Hello, In my case I have only two possibilities so I will use for select even if it is not clear Thanks alot Maxim I learned something new
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads