LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-05-2015, 01:27 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
db2 SQL question


Not sure if this is a programming question.

I have 2 tables with the following content

Code:
$ db2 "select * from C"

FRUIT    SN
-------  --
apple     4
apple     5
apple     6
banana   22
banana   23

5 records selected

$ db2 "select fruit, min(sn) as min_sn from U group by fruit"

FRUIT    MIN_SN
-------  ------
apple         7
banana       15
pear         88

3 records selected
Note that 'pear' does not appear in table C.
Table U contains all fruit entries in this universe.

I want to count each type of fruit (apple, banana, pear) in table C that has Serial Number (SN) smaller than the MIN_SN returned from the 2nd query. Is there is efficient way to do it? The tricky part is that the banana and pear count should be 0.

Code:
$ db2 "
select fruit, count(sn) as count
from (
  select MU.fruit, C.sn
  from C
  right join (select fruit, min(sn) as min_sn from U group by fruit) MU
  on C.fruit = MU.fruit
  where C.sn < MU.min_sn
)
group by fruit"

FRUIT    COUNT
-------  -----
apple        3

1 records selected
The above query only shows apple having 3 counts, but no banana or pear. So a union is added:

Code:
$ db2 "
select *
from (
  select fruit, count(sn) as count, min_sn
  from (
    select MU.fruit, C.sn, MU.min_sn
    from C
    right join (select fruit, min(sn) as min_sn from U group by fruit) MU
    on C.fruit = MU.fruit
    where C.sn < MU.min_sn
  )
  group by fruit, min_sn
)
union
(
  select fruit, 0 as count, min(sn) as min_sn
  from U
  where not exists (select 1
                    from C
                    where fruit = U.fruit)
  group by fruit
)
order by fruit"

FRUIT    COUNT  MIN_SN
-------  -----  ------
apple        3       7
banana       0      15
pear         0      88

3 records selected
This is quite clumsy, don't you think?
 
Old 11-21-2015, 04:03 PM   #2
dr_agon
Member
 
Registered: Sep 2007
Location: Poland
Distribution: Ubuntu LTS
Posts: 105
Blog Entries: 12

Rep: Reputation: 26
I would just nest the SELECTs like this:

Code:
select 
  fruit, 
  min(sn) as min_sn, 
  (select count(C.sn) from C where C.fruit = U.fruit and C.sn < min(U.sn) ) as C_count 
from U 
group by fruit
Look at the example for Postgres (I do not know db2): http://sqlfiddle.com/#!15/7bf9f/20
 
1 members found this post helpful.
Old 11-23-2015, 02:11 AM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Right. Thanks. I didn't know sub-SELECT could go right after SELECT.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
DB2 user needs help using Oracle SQL*Plus beeblequix Linux - Enterprise 1 06-18-2012 11:11 AM
SQL Problem on DB2 Sparkle1984 Linux - Software 2 06-28-2010 04:49 AM
LXer: Use XML in DB2 SQL stored procedures LXer Syndicated Linux News 0 01-20-2007 04:03 AM
IBM DB2 sql troubles Reegz Programming 0 12-14-2006 01:54 AM
LXer: Mix and Match SQL and XML with DB2 Viper hybrid LXer Syndicated Linux News 0 02-17-2006 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration