473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to get the zero's for unsatisfied fields in group by clause in DB2

2
Hi All,

I have pasted my DB2 Query and result below.

db2 => SELECT tp.COUNTRY, tp.KIND,count(tp.patent_key) as "COUNT OF BASICS" FROM PATENT AS tp, TRACKER AS tr WHERE (tp.PATENT_KEY=tr.PATENT_KEY) AND (tr.KEY in (SELECT key from TRACKER_DICT where STATE_NAME='Create New Patent Record')) AND (tr.ARRIVED >='2004-06-22-00.00.00.000000' and tr.ARRIVED < '2008-06-23-00.00.00.000000') AND tp.basic_type='B' GROUP BY tp.COUNTRY, tp.KIND

COUNTRY KIND COUNT OF BASICS
------------ --------- ---------------
CA A1 1
ID A2 1
MX A1 1
WO A1 1
WO A2 1

5 record(s) selected.


But i need to display zero for all the country and kind combinations which doesn't satisfy the conditions in the where clause.

Requirement is like below:
COUNTRY KIND COUNT OF BASICS
AU A1 0
CA A1 1
DK A1 0
GB A1 0
GB A2 0
ID A2 1
IN A1 0
JP A1 0
MX A1 1
MX B1 0
WO A1 1
WO A2 1
ZA A 0
ZW A1 0


Please help me in this.
Thanks in advance.
Aug 20 '07 #1
1 1401
Hi All,

I have pasted my DB2 Query and result below.

db2 => SELECT tp.COUNTRY, tp.KIND,count(tp.patent_key) as "COUNT OF BASICS" FROM PATENT AS tp, TRACKER AS tr WHERE (tp.PATENT_KEY=tr.PATENT_KEY) AND (tr.KEY in (SELECT key from TRACKER_DICT where STATE_NAME='Create New Patent Record')) AND (tr.ARRIVED >='2004-06-22-00.00.00.000000' and tr.ARRIVED < '2008-06-23-00.00.00.000000') AND tp.basic_type='B' GROUP BY tp.COUNTRY, tp.KIND

COUNTRY KIND COUNT OF BASICS
------------ --------- ---------------
CA A1 1
ID A2 1
MX A1 1
WO A1 1
WO A2 1

5 record(s) selected.


But i need to display zero for all the country and kind combinations which doesn't satisfy the conditions in the where clause.

Requirement is like below:
COUNTRY KIND COUNT OF BASICS
AU A1 0
CA A1 1
DK A1 0
GB A1 0
GB A2 0
ID A2 1
IN A1 0
JP A1 0
MX A1 1
MX B1 0
WO A1 1
WO A2 1
ZA A 0
ZW A1 0


Please help me in this.
Thanks in advance.
Assuming that you're missing some rows based on a non-match in the key columns, the following statement should provide the desired results.
SELECT tp.COUNTRY, tp.KIND,count(tp.patent_key) as "COUNT OF BASICS" FROM PATENT AS tp, left outer join TRACKER AS tr on tp.PATENT_KEY=tr.PATENT_KEY where (tr.KEY in (SELECT key from TRACKER_DICT where STATE_NAME='Create New Patent Record')) AND (tr.ARRIVED >='2004-06-22-00.00.00.000000' and tr.ARRIVED < '2008-06-23-00.00.00.000000') AND tp.basic_type='B' GROUP BY tp.COUNTRY, tp.KIND

Hope this helps,

Fritz
Aug 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
3
by: kattukuyil | last post by:
Guys, this is what I want to do. How can I do this ? Select employid, Gross=( Select sum (uprtrxam) from Fleet..upr30300 where pyrlrtyp=1), DedofWages=( Select sum (uprtrxam) from...
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
1
by: florian.boldt | last post by:
Hi Folks, one of our developers uses a statement with a where clause which usually does not match to any rows. In case of one or more rows found she wrote a an expression in the select clause...
5
by: Salad | last post by:
This may occur to you in the future. You want to get the min or max of a field that you set criteria on...maybe for a combo box. Let's say you wanted to get the minimum of all date fields where...
3
by: Icemokka | last post by:
Hi, I've got a table in MsAccess with 100+ fields. If I fill a tabletable with this table , change some values , get the update-command via commandbuilder , the update fails. This because the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.