Query for selecting NON DUPLICATE ELEMENTS in a table 
October 28th, 2008, 05:35 PM
| | | |
What is the query for selecting non duplicate elements
for eg:
no name age
1 siva 28
2 blair 32
3 mano 28
i want to select blair which hasn't got any duplicate elements in age
column.
Thx in advance | 
October 28th, 2008, 09:05 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote: Quote:
What is the query for selecting non duplicate elements
>
for eg:
>
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
>
i want to select blair which hasn't got any duplicate elements in age
column.
>
| One possibility:
db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
NO NAME AGE
----------- ----- -----------
2 blair 32
1 record(s) selected.
/Lennart | 
October 28th, 2008, 09:25 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote: Quote:
What is the query for selecting non duplicate elements
>
for eg:
>
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
>
i want to select blair which hasn't got any duplicate elements in age
column.
>
Thx in advance
| Not sure what happened to my post, here is one attempt:
db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
NO NAME AGE
----------- ----- -----------
2 blair 32
1 record(s) selected.
/Lennart | 
October 29th, 2008, 12:45 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
Is this going to be such complex for retrieval of simple data
isn't there any function for this ? just like distinct ?
or can it be added to newer versions of db2 ?
On Oct 28, 8:16*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote: Quote:
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote:
> Quote: |
What is the query for selecting non duplicate elements
| >> Quote:
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
| > Quote:
i want to select blair which hasn't got any duplicate elements in age
column.
| >>
Not sure what happened to my post, here is one attempt:
>
*db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
>
NO * * * * *NAME *AGE
----------- ----- -----------
* * * * * 2 blair * * * * *32
>
* 1 record(s) selected.
>
/Lennart
| | 
October 29th, 2008, 06:15 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
On Oct 29, 4:43*am, swami <sivaswamim...@gmail.comwrote: Quote:
Is this going to be such complex for retrieval of simple data
isn't there any function for this ? just like distinct ?
or can it be added to newer versions of db2 ?
>
On Oct 28, 8:16*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
> Quote: |
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote:
| > Quote: Quote: |
What is the query for selecting non duplicate elements
| | >> Quote: Quote:
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
| | > Quote: Quote:
i want to select blair which hasn't got any duplicate elements in age
column.
| | >> Quote: |
Not sure what happened to my post, here is one attempt:
| > Quote:
*db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
| > Quote:
NO * * * * *NAME *AGE
----------- ----- -----------
* * * * * 2 blair * * * * *32
| >>>
>
| Another way:
SELECT
NO,
NAME,
AGE
FROM
(
SELECT
NO,
NAME,
AGE,
ROW_NUMBER() OVER (PARTITION BY AGE) RN
FROM
DEMOG_TABLE
) DT
WHERE
RN = 1;
DISTINCT won't work (at least in a straightforward way) because you
can't use it to simultaneously retrieve several columns and apply the
DISTINCT on just a subset of them.
--Jeff | 
October 29th, 2008, 06:25 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
jefftyzzer wrote: Quote:
On Oct 29, 4:43Â*am, swami <sivaswamim...@gmail.comwrote: Quote:
>Is this going to be such complex for retrieval of simple data
>isn't there any function for this ? just like distinct ?
>or can it be added to newer versions of db2 ?
>>
>On Oct 28, 8:16Â*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
>> Quote: |
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote:
| >> Quote: |
What is the query for selecting non duplicate elements
| >>>> Quote:
no Â* name age
1 Â* Â* siva Â* Â*28
2 Â* Â* blair Â* Â*32
3 Â* Â* mano Â*28
| >> Quote:
i want to select blair which hasn't got any duplicate elements in age
column.
| >>>> Quote: |
Not sure what happened to my post, here is one attempt:
| >> Quote:
db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
| >> Quote:
NO Â* Â* Â* Â* Â*NAME Â*AGE
----------- ----- -----------
2 blair Â* Â* Â* Â* Â*32
| >>>>>>
>>
| >
Another way:
>
SELECT
NO,
NAME,
AGE
FROM
(
SELECT
NO,
NAME,
AGE,
ROW_NUMBER() OVER (PARTITION BY AGE) RN
FROM
DEMOG_TABLE
) DT
WHERE
RN = 1;
>
DISTINCT won't work (at least in a straightforward way) because you
can't use it to simultaneously retrieve several columns and apply the
DISTINCT on just a subset of them.
| Your solution runs a good deal faster here - I've used both to update a
database I maintain for a non-profit group membership list. It's amazing
how many duplicate names appear in successive generations attending one
school
--
Will Honea
** Posted from http://www.teranews.com ** | 
October 29th, 2008, 08:05 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
On Oct 29, 10:19*am, "Will Ho...@teranews.com" <who...@yahoo.com>
wrote: Quote:
jefftyzzer wrote: Quote:
On Oct 29, 4:43*am, swami <sivaswamim...@gmail.comwrote: Quote:
Is this going to be such complex for retrieval of simple data
isn't there any function for this ? just like distinct ?
or can it be added to newer versions of db2 ?
| | > Quote: Quote: |
On Oct 28, 8:16*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
| | > Quote: Quote: |
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote:
| | > Quote: Quote: |
What is the query for selecting non duplicate elements
| | >> Quote: Quote:
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
| | > Quote: Quote:
i want to select blair which hasn't got any duplicate elements in age
column.
| | >> Quote: Quote: |
Not sure what happened to my post, here is one attempt:
| | > Quote: Quote:
db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
| | > Quote: Quote:
NO * * * * *NAME *AGE
----------- ----- -----------
2 blair * * * * *32
| | >>>> Quote:
SELECT
* * NO,
* * NAME,
* * AGE
FROM
* * (
* * SELECT
* * * * NO,
* * * * NAME,
* * * * AGE,
* * * * ROW_NUMBER() OVER (PARTITION BY AGE) RN
* * FROM
* * * * DEMOG_TABLE
* * ) DT
WHERE
* * RN = 1;
| > Quote:
DISTINCT won't work (at least in a straightforward way) because you
can't use it to simultaneously retrieve several columns and apply the
DISTINCT on just a subset of them.
| >
Your solution runs a good deal faster here - I've used both to update a
database I maintain for a non-profit group membership list. *It's amazing
how many duplicate names appear in successive generations attending one
school
>
--
Will Honea
** Posted fromhttp://www.teranews.com**
| Swami:
It occurs to me that I may not have answered the right question. If
what you want is the first instance, duplicated or otherwise, then you
can use the query I originally posted. If, however, what you want is
to retrieve just those rows that are not/never were duplicated over
the age column, then this query is likely what you're after:
SELECT
NO,
NAME,
AGE
FROM
(
SELECT
NO,
NAME,
AGE,
COUNT() OVER (PARTITION BY AGE) CNT
FROM
DEMOG_TABLE
) DT
WHERE
CNT = 1;
Will: thanks for your comment. Glad to know you're doing your part to
ensure high-quality data!
Regards,
--Jeff | 
October 29th, 2008, 08:25 PM
| | | | re: Query for selecting NON DUPLICATE ELEMENTS in a table
On Oct 29, 10:19*am, "Will Ho...@teranews.com" <who...@yahoo.com>
wrote: Quote:
jefftyzzer wrote: Quote:
On Oct 29, 4:43*am, swami <sivaswamim...@gmail.comwrote: Quote:
Is this going to be such complex for retrieval of simple data
isn't there any function for this ? just like distinct ?
or can it be added to newer versions of db2 ?
| | > Quote: Quote: |
On Oct 28, 8:16*pm, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
| | > Quote: Quote: |
On 28 Okt, 17:25, swami <sivaswamim...@gmail.comwrote:
| | > Quote: Quote: |
What is the query for selecting non duplicate elements
| | >> Quote: Quote:
no * name age
1 * * siva * *28
2 * * blair * *32
3 * * mano *28
| | > Quote: Quote:
i want to select blair which hasn't got any duplicate elements in age
column.
| | >> Quote: Quote: |
Not sure what happened to my post, here is one attempt:
| | > Quote: Quote:
db2 "with T (no, name, age) as (values (1,'siva',28), (2,'blair',32),
(3,'mano',28)) select T.* from T where age in (select age from T group
by age having count(1) = 1)"
| | > Quote: Quote:
NO * * * * *NAME *AGE
----------- ----- -----------
2 blair * * * * *32
| | >>>> Quote:
SELECT
* * NO,
* * NAME,
* * AGE
FROM
* * (
* * SELECT
* * * * NO,
* * * * NAME,
* * * * AGE,
* * * * ROW_NUMBER() OVER (PARTITION BY AGE) RN
* * FROM
* * * * DEMOG_TABLE
* * ) DT
WHERE
* * RN = 1;
| > Quote:
DISTINCT won't work (at least in a straightforward way) because you
can't use it to simultaneously retrieve several columns and apply the
DISTINCT on just a subset of them.
| >
Your solution runs a good deal faster here - I've used both to update a
database I maintain for a non-profit group membership list. *It's amazing
how many duplicate names appear in successive generations attending one
school
>
--
Will Honea
** Posted fromhttp://www.teranews.com**
| Swami:
It occurs to me that I may not have answered the right question. If
what you want is the first instance, duplicated or otherwise, then you
can use the query I originally posted. If, however, what you want is
to retrieve just those rows that are not/never were duplicated over
the age column, then this query is likely what you're after:
SELECT
NO,
NAME,
AGE
FROM
(
SELECT
NO,
NAME,
AGE,
COUNT(*) OVER (PARTITION BY AGE) CNT
FROM
DEMOG_TABLE
) DT
WHERE
CNT = 1;
Will:
Thanks for your comment. Glad to know you're doing your part to
ensure high-quality data!
Regards,
--Jeff |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|