473,406 Members | 2,220 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,406 software developers and data experts.

Newbee Query Problem

Hi,

Being new to DB2 I'm having a small problem that I'm sure is quite easy to someone smarter than me.

I have a table with duplicate entries and I need to know how to code a query to exctract certain fields from the table with a count > 1. I can do this with one field however I need to do this with multiple fields.

Working 1 Field Query,
Expand|Select|Wrap|Line Numbers
  1.  select field_3 from CUSTOMER where field_3 <> ' ' group by field_3 having count(*) > 1 
What doesn't work,
Expand|Select|Wrap|Line Numbers
  1.  select * from CUSTOMER where field_3 <> ' ' group by field_3 having count(*) > 1 
Table Name: CUSTOMER
# of Fields: 5 (e.g. Filed_1, Field_2, Field_3, etc)

If some could help me with this I would be eternally grateful.

Thanks in advance.......
Jul 23 '07 #1
2 1247
Provided you are on V8.1 or later, try the following:

Expand|Select|Wrap|Line Numbers
  1. create table test(col1 char(3),
  2.                   col2 char(3),
  3.                   col3 char(3));
  4.  
  5. insert into test values ('1A','1B','1C');
  6. insert into test values ('2A','2B','2C');
  7. insert into test values ('3A','3B','3C');
  8. insert into test values ('3A','3B','3C');
  9. insert into test values ('3A','3B','3C');
  10. insert into test values ('4A','4B','4C');
  11. insert into test values ('5A','5B','5C');
  12. insert into test values ('5A','5B','5C');
  13.  
  14. with x(rnum, col1, col2, col3) as (select rownumber() over(order by col1, col2, col3),
  15.                                           col1,
  16.                                           col2,
  17.                                           col3
  18.                                      from test)
  19. select distinct x1.col1,
  20.                 x1.col2,
  21.                 x1.col3
  22.   from x x1,
  23.        x x2
  24.  where x1.col1 = x2.col1
  25.    and x1.col2 = x2.col2
  26.    and x1.col3 = x2.col3
  27.    and x1.rnum <> x2.rnum
Aug 2 '07 #2
Thinking some more on this, there is a much simpler way:

Expand|Select|Wrap|Line Numbers
  1. select distinct col1,
  2.                 col2,
  3.                 col3
  4.   from test
  5.  group by col1,
  6.           col2,
  7.           col3
  8.  having count(*) > 1
Aug 3 '07 #3

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

Similar topics

6
by: James Turner | last post by:
I am trying to store formatted text (windows format) into a MySQL database and then retrieve it. The field in the database is a varchar. I cut and paste the test into a form field formatted, then...
3
by: Newbee | last post by:
Hi ! Let's say that this is the folder on the server: /web/firstDir/secondDir/images/image.gif where i have stored my pictures. I have tryed with apsolute and relative paths but i can't display...
2
by: Newbee Adam | last post by:
some said that .NET app can run on any program where rutime exists. What is "runtime" in this sense? will I have to install runtime or .net framework or .NET support on an xp machine for a...
1
by: Magnus | last post by:
Hi, I have previously worked mostly with Sql Server and wonder how to write a subselect query in mysql 4.0. What i would like to do is something like: select a.name, b.isbn from author a...
4
by: PerryC | last post by:
All, 1. Do the following codes seem ok? 2. If so, then how do I pull the value of YOE1 and YOE2 into my report? (to do some further calculations) ...
2
by: Martin Hvidberg | last post by:
Dear list I have found a declaration like this: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include "ectemp.h"
6
by: tony | last post by:
Hello, I am trying to loop through a collection of objects, create a sql string from data held within those objects and insert into a database. Each time through the loop the program seems to...
8
by: ikarias | last post by:
Maybe not the right place to aske this, but a newbee (me) needs help. i trying to input a number into a textbox, so i can make a series of calculations. in the old days of basic i just jused...
2
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.