Article View: borland.public.interbase.sql
Article #1015Re: Combine two queries
From: Wayne Niddery [TeamB]
Date: Tue, 06 Mar 2007 15:21
Date: Tue, 06 Mar 2007 15:21
40 lines
2627 bytes
2627 bytes
Hs wrote:
>
> 1. SELECT SUM(BUY.WEIGHT), SUM(BUY.WEIGHT) * 100
> FROM BUY WHERE BUY.PAID = 'N' GROUP BY BUY.FARMERID
>
> 2. SELECT FARMER.* FROM FARMER WHERE FARMER.ID IN
> (SELECT BUY.FARMERID FROM BUY WHERE BUY.PAID = 'N' GROUP BY
> BUY.FARMERID)
>
> - In first query I have to use SUM(BUY.WEIGHT) twice. will it
> decrease performance? if yes how can I use it once?
If this is going to a report, the report engine could calculate the second
sum * 100. It could also be done in the application instead of the query.
However I doubt the performance different willr eally be noticeable. You can
test it by looking at the stats for the query in IBConsole.
> - Is there any way to combine thease two queries into one query?
> (their condition is same)
Assuming there is only going to be one farmer for each BUY record, then yes.
The group by will need to include all fields from farmer that you select, so
select the fewest possible:
SELECT
FARMER.ID, FARMER.NAME, FARMER.CITY, <etc>
SUM(BUY.WEIGHT), SUM(BUY.WEIGHT) * 100
FROM FARMER
INNER JOIN BUY ON BUY.FARMERID = FARMER.ID
WHERE BUY.PAID = 'N'
GROUP BY BUY.FARMERID, FARMER.NAME, FARMER.CITY, <etc>
You should always use joins when possible instead of subqueries. Joins will
be much faster in most cases.
--
Wayne Niddery - Winwright, Inc (www.winwright.ca)
"Those who disdain wealth as a worthy goal for an individual or a
society seem not to realize that wealth is the only thing that can
prevent poverty." - Thomas Sowell
Technical Details
Message-ID:
<45ed86ca$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.news2me.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<45ed7043$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,fecb26f87dac9ab0 X-Google-Attributes: gid114a0e,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Wayne Niddery [TeamB]" <wniddery@chaffaci.on.ca> From: "Wayne Niddery [TeamB]" <wniddery@chaffaci.on.ca> Newsgroups: borland.public.interbase.sql References: <45ed7043$1@newsgroups.borland.com> Subject: Re: Combine two queries Date: Tue, 6 Mar 2007 10:21:05 -0500 Lines: 42 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 69.17.170.228 Message-ID: <45ed86ca$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1173194442 69.17.170.228 (6 Mar 2007 07:20:42 -0800) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.news2me.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail