Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Unique values in column

 
   Database Help (Home) -> DB2 RSS
Next:  Form viewed -history  
Author Message
Dawid Zolkiewicz

External


Since: Oct 25, 2008
Posts: 5



(Msg. 1) Posted: Sat Oct 25, 2008 10:25 am
Post subject: Unique values in column
Archived from groups: comp>databases>ibm-db2 (more info?)

Hello

I'm new here, so at the beginning I'd like to say hello for everybody.

First I'll describe my problem.

There is db2 database Smile with about 0.5 mln people. Every person has
three features. These features have allowable values and also are stored
in db. I have to prepare query to find all possible combinations ok there
features and for every combination find one person having them.

So far I have query to find combinations but I can't limit number of
people to 1. My idea was to connect values from three columns to one, so
I have:

+-----------+-----------+-----------+-----------+-----------+
|feature 1 |feature 2 |feature 3 |1+2+3 |prsn ID |
+-----------+-----------+-----------+-----------+-----------+
| A | B | C | A/B/C | |
+-----------+-----------+-----------+-----------+-----------+
| D | E | F | D/E/F | |
+-----------+-----------+-----------+-----------+-----------+

And my idea was to force query to show in table only unique values in 4th
column. But I don't know how to do that. Unfortunately, for some reason
option ROW_NUMBER doesn't work.

I'll be very thankful for any ideas.

Dawid

 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Ian7

External


Since: Sep 15, 2003
Posts: 225



(Msg. 2) Posted: Sat Oct 25, 2008 10:49 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dawid Zolkiewicz wrote:
> Hello
>
> I'm new here, so at the beginning I'd like to say hello for everybody.
>
> First I'll describe my problem.
>
> There is db2 database Smile with about 0.5 mln people. Every person has
> three features. These features have allowable values and also are stored
> in db. I have to prepare query to find all possible combinations ok there
> features and for every combination find one person having them.
>
> So far I have query to find combinations but I can't limit number of
> people to 1. My idea was to connect values from three columns to one, so
> I have:
>
> +-----------+-----------+-----------+-----------+-----------+
> |feature 1 |feature 2 |feature 3 |1+2+3 |prsn ID |
> +-----------+-----------+-----------+-----------+-----------+
> | A | B | C | A/B/C | |
> +-----------+-----------+-----------+-----------+-----------+
> | D | E | F | D/E/F | |
> +-----------+-----------+-----------+-----------+-----------+
>
> And my idea was to force query to show in table only unique values in 4th
> column. But I don't know how to do that. Unfortunately, for some reason
> option ROW_NUMBER doesn't work.
>

You don't say whether a person that has A/B/C is the same as a person
that has C/B/A, but let's assume they are different.

SELECT prsnID ,
feature1,
feature2,
feature3
FROM
(SELECT prsnID ,
feature1,
feature2,
feature3,
ROWNUMBER() OVER (PARTITION BY feature1,
feature2,
feature3) AS rn
FROM people
) AS p
WHERE rn = 1;

 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Dawid Zolkiewicz

External


Since: Oct 25, 2008
Posts: 5



(Msg. 3) Posted: Sun Oct 26, 2008 12:25 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

For some reason I can't use rownumber - I have error:

ERROR[42884][IBM][DB2] SQL0440N No autorized routine named "ROWNUMBER"
of type "" having compatible arguments was found.

I found information on some forum that option doesn't work in Mainframe
(I'm using that).

David
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Dawid Zolkiewicz

External


Since: Oct 25, 2008
Posts: 5



(Msg. 4) Posted: Sun Oct 26, 2008 1:28 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
Mark A

External


Since: Aug 29, 2008
Posts: 48



(Msg. 5) Posted: Sun Oct 26, 2008 4:47 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Dawid Zolkiewicz" <j_e_l_l_o_w DeleteThis @tenbit.pl> wrote in message
news:ge0qq1$7pc$1@aioe.org...
> For some reason I can't use rownumber - I have error:
>
> ERROR[42884][IBM][DB2] SQL0440N No autorized routine named "ROWNUMBER"
> of type "" having compatible arguments was found.
>
> I found information on some forum that option doesn't work in Mainframe
> (I'm using that).
>
> David

Did you ever consider consulting the SQL Reference Manual to see if it is
supported on the DB2 platform and version you are using?
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1069



(Msg. 6) Posted: Sun Oct 26, 2008 6:23 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"A problem well stated is a problem half solved." -- Charles F.
Kettering

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.

Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html

>> And my idea was to force query to show in table only unique values in 4th column. But I don't know how to do that. Unfortunately, for some reason option ROW_NUMBER doesn't work. <<

Your fourth column is not in First Normal Form and it is redundant.

This sounds like an OUTER JOIN on a three-way CROSS JOIN, but without
DDL, we cannot write a query.
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Dawid Zolkiewicz

External


Since: Oct 25, 2008
Posts: 5



(Msg. 7) Posted: Sun Oct 26, 2008 12:25 pm
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Please post DDL,

I can't do that, because I don't have access to documentation like that
(I'm only little user), but I prepared schema of interesting tables and
expected result.

Feature1_dfn
+-----------+------------+
|Feature1_ID|Feature1_DSC|
+-----------+------------+
|INT |STR |
+-----------+------------+
|1 |plan1 |
+-----------+------------+
|2 |plan2 |
+-----------+------------+
|3 |plan3 |
+-----------+------------+

Feature2_3_dfn
+-------------+--------------------+
|Feature2_3_ID|Feature2_3_vld_value|
+-------------+--------------------+
|INT |STR |
+-------------+--------------------+
|1 |option1 |
+-------------+--------------------+
|1 |option2 |
+-------------+--------------------+
|1 |option3 |
+-------------+--------------------+
|1 |option4 |
+-------------+--------------------+
|2 |variant1 |
+-------------+--------------------+
|2 |variant2 |
+-------------+--------------------+
|2 |variant3 |
+-------------+--------------------+
|2 |variant4 |
+-------------+--------------------+

PRSN_FEATURE1
+--------+-----------+
|PRSN_ID |Feature1_ID|
+--------+-----------+
|INT |INT |
+--------+-----------+
|0001 |1 |
+--------+-----------+
|0002 |1 |
+--------+-----------+
|0002 |2 |
+--------+-----------+

PRSN_FEATURE2_3
+-------+------------------+---------------------+
|PRSN_ID|PRSN_FEATURE2_3_ID|PRSN_FEATURE2_3_VALUE|
+-------+------------------+---------------------+
|0001 |1 |option2 |
+-------+------------------+---------------------+
|0001 |2 |variant1 |
+-------+------------------+---------------------+
|0002 |1 |option3 |
+-------+------------------+---------------------+
|002 |2 |variant4 |
+-------+------------------+---------------------+

QUERY
+-----------+-------------------------+-------------------------+-------+
|Feature1_ID|FEATURE2_3_VALUE (ID = 1)|FEATURE2_3_VALUE (ID = 2)|PRSN_ID|
+-----------+-------------------------+-------------------------+-------+
|plan1 |option1 |variant1 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option1 |variant2 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option1 |variant3 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option1 |variant4 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option2 |variant1 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option2 |variant2 | |
+-----------+-------------------------+-------------------------+-------+
|plan1 |option2 |variant3 | |
+-----------+-------------------------+-------------------------+-------+

Tables *_dfn collect allowable, valid values of these three features.

Tables PRSN_* collect informations about people

PRSN_IDs found in query don't have to be unique (usually one person has a
few plans, one option and one variant).

Found person can be random, first or last.

Problem is - possible combinations of feature is about 30.000 and
possible combinations of futures and people about 3.000.000.

Thank you for your help

Dawid
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Dawid Zolkiewicz

External


Since: Oct 25, 2008
Posts: 5



(Msg. 8) Posted: Sun Oct 26, 2008 1:25 pm
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Did you ever consider consulting the SQL Reference Manual to see if it
> is supported on the DB2 platform and version you are using?

I working remotely with mainframe and I couldn't determine what version
we are using. My co-worker said is probably 6th one. Is any option to
don't use ROWNUMBER?

Dawid
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Arun Srinivasan

External


Since: Feb 05, 2008
Posts: 27



(Msg. 9) Posted: Sun Oct 26, 2008 11:11 pm
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Oct 25, 8:41 am, Dawid Zolkiewicz <j_e_l_l_....TakeThisOut@tenbit.pl> wrote:
> Hello
>
> I'm new here, so at the beginning I'd like to say hello for everybody.
>
> First I'll describe my problem.
>
> There is db2 database Smile with about 0.5 mln people. Every person has
> three features. These features have allowable values and also are stored
> in db. I have to prepare query to find all possible combinations ok there
> features and for every combination find one person having them.
>
> So far I have query to find combinations but I can’t limit number of
> people to 1. My idea was to connect values from three columns to one, so
> I have:
>
> +-----------+-----------+-----------+-----------+-----------+
> |feature 1  |feature 2  |feature 3  |1+2+3      |prsn ID    |
> +-----------+-----------+-----------+-----------+-----------+
> |     A     |     B     |     C     | A/B/C     |           |
> +-----------+-----------+-----------+-----------+-----------+
> |     D     |     E     |     F     | D/E/F     |           |
> +-----------+-----------+-----------+-----------+-----------+
>
> And my idea was to force query to show in table only unique values in 4th
> column. But I don't know how to do that. Unfortunately, for some reason
> option ROW_NUMBER doesn't work.
>
> I'll be very thankful for any ideas.
>
> Dawid

The following is just a skeletal one to show how your query can be
outlined, please customize it to your need.
The table arun.people_test has 4 columns, name, q1,q2,q3.
db2 "select * from arun.people_test"

Q1 Q2 Q3 NAME
-- -- -- -------------------------
a a a arun
a b b sandy
a b b sandy1
a a a arun1

4 record(s) selected.

with temp1(q1,q2,q3) as
(select q1,q2,q3 from arun.people_test group by q1,q2,q3),
temp2(q1,q2,q3,name,r) as (select t.q1,t.q2,t.q3,p.name,row_number()
over(partition by t.q1,t.q2,t.q3) as r from temp1 t,arun.people_test
p where t.q1=p.q1 and t.q2=p.q2 and t.q3 = p.q3)
select * from temp2 where r=1


Q1 Q2 Q3 NAME R
-- -- -- ------------------------- --------------------
a a a arun 1
a b b sandy 1
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
ChrisC

External


Since: May 11, 2007
Posts: 10



(Msg. 10) Posted: Tue Oct 28, 2008 8:20 am
Post subject: Re: Unique values in column [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Oct 25, 10:49 am, Ian <ianb....DeleteThis@mobileaudio.com> wrote:
> SELECT prsnID  ,
>         feature1,
>         feature2,
>         feature3
> FROM
>         (SELECT prsnID ,
>                feature1,
>                feature2,
>                feature3,
>                ROWNUMBER() OVER (PARTITION BY feature1,
>                                               feature2,
>                                               feature3) AS rn
>         FROM   people
>         ) AS p
> WHERE  rn = 1;- Hide quoted text -
>
> - Show quoted text -

Actually, the function is ROW_NUMBER() - you need the underbar. Of
course, it still may not work depending on version.

-Chris
 >> Stay informed about: Unique values in column 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> DB2 All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]