If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > subtracting 2 columns, with the group by clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-11, 06:31
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
subtracting 2 columns, with the group by clause

Hi,
I have two select statements

Stmt1:

Code:
Select count(*) count, fruit from table1 group by fruit

Result:

count-----fruit

12--------Apple
34--------Orange
24--------Melon
Stmt2:

Code:
Select count(*) count, fruit from table2 group by fruit

Result:

count-----fruit

10--------Apple
30--------Orange
20--------Melon
Can some one please help me with a select statement which gives me the difference between the above two results, with the group by clause intact

Result::

Code:
count-----fruit

2--------Apple
4--------Orange
4--------Melon

thanks in advace!
Reply With Quote
  #2 (permalink)  
Old 06-23-11, 07:08
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
SELECT t1.count - t2.count, t1.fruit
FROM ( <query1> ) AS t1 JOIN ( <query2> ) AS t2 ON ( t1.fruit = t2.fruit )
Just remember: pretty much everything in SQL is a table. A query produces a table as output, and you can use a table in the FROM clause of a query, for example. Just combine these two things and you have subselects.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On