If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > select self join, distinct values

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-10, 16:59
dform dform is offline
Registered User
 
Join Date: Sep 2010
Posts: 5
select self join, distinct values

I have no idea what sql command will return the results I need from 1 table.

example of table and values

col1 col2 col3(unique values)
1 1 abc
1 1 dev
1 2 rst
1 3 ndb
1 3 mnv

I need an sql query, that returns the distinct combined values of col1
and col2 and the first value of that distinct combination from col3.


return values should be;
1 1 abc
1 2 rst
1 3 ndb
Reply With Quote
  #2 (permalink)  
Old 09-10-10, 18:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by dform View Post
the first value of that distinct combination from col3.
Please define "first". Rows in a table do not have any intrinsic ordering.
Reply With Quote
  #3 (permalink)  
Old 09-10-10, 20:12
dform dform is offline
Registered User
 
Join Date: Sep 2010
Posts: 5
First row meaning. Return a c value for each distinct a,b combination
Reply With Quote
  #4 (permalink)  
Old 09-10-10, 20:22
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Please define "first". Rows in a table do not have any intrinsic ordering.
Yes, many experts metined repeatedly that point.

Because, DB2(and almost all relational databases) don't expect any order of rows in a table,
even if you supplied or showed rows with specific order.

The logical order of rows have meaning only in a query using ORDER BY clause explicitly.


Here are examples.

Exampel 1:
'ndb' was selected for a row (col1, col2) = (1, 3).
Code:
------------------------------ Commands Entered ------------------------------
SELECT col1 , col2 , col3
 FROM  (SELECT s.*
             , ROW_NUMBER()
                 OVER(PARTITION BY col1, col2) rn
         FROM  dform.sample_data s
       ) t
 WHERE rn = 1
;
------------------------------------------------------------------------------

COL1        COL2        COL3   
----------- ----------- -------
          1           1 abc    
          1           2 rst    
          1           3 ndb    

  3 record(s) selected.

Exampel 2:
'mnv' was selected for a row (col1, col2) = (1, 3).
Code:
------------------------------ Commands Entered ------------------------------
ALTER TABLE dform.sample_data
ADD PRIMARY KEY (col1 , col2 , col3)
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT col1 , col2 , col3
 FROM  (SELECT s.*
             , ROW_NUMBER()
                 OVER(PARTITION BY col1, col2) rn
         FROM  dform.sample_data s
       ) t
 WHERE rn = 1
;
------------------------------------------------------------------------------

COL1        COL2        COL3   
----------- ----------- -------
          1           1 abc    
          1           2 rst    
          1           3 mnv    

  3 record(s) selected.

DDL and data:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE dform.sample_data
( col1 INTEGER    NOT NULL
, col2 INTEGER    NOT NULL
, col3 VARCHAR(7) NOT NULL
);
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
INSERT INTO dform.sample_data
VALUES
  (1 , 1 , 'abc')
, (1 , 1 , 'dev')
, (1 , 2 , 'rst')
, (1 , 3 , 'ndb')
, (1 , 3 , 'mnv')
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.
Reply With Quote
  #5 (permalink)  
Old 09-10-10, 22:41
dform dform is offline
Registered User
 
Join Date: Sep 2010
Posts: 5
Wow, I am not familiar with OLAP functions, over(partition function. I must check the version to find out if it is supported. Is your first example a solution, I cannot test this at the moment.


Are there other ways to perform return these same results with basic inner/outer joins or unions with a dummy table?
Reply With Quote
  #6 (permalink)  
Old 09-11-10, 01:56
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Is your first example a solution, ...
It was by chance that the result of Example 1 looks like what you wanted.

Please compare the results of Example 1 and Example2.
I want to show the result was changed by adding PRIMARY KEY(and created corresponding index implicitly),
if ORDER BY was not specified,
even when two queries and data were completely identical.

Last edited by tonkuma; 09-11-10 at 02:04.
Reply With Quote
  #7 (permalink)  
Old 09-11-10, 21:31
dform dform is offline
Registered User
 
Join Date: Sep 2010
Posts: 5
thank you.

The result returned by either example for col 3 is sufficient for the purpose of this exercise. It would be best to have the same results without using the OLAP function.
Reply With Quote
  #8 (permalink)  
Old 09-11-10, 22:40
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
It would be best to have the same results without using the OLAP function.
Why is it best?
Reply With Quote
  #9 (permalink)  
Old 09-12-10, 09:40
dform dform is offline
Registered User
 
Join Date: Sep 2010
Posts: 5
The olap function is probably not supported in the version I use.
Reply With Quote
  #10 (permalink)  
Old 09-12-10, 10:02
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What are your DB2 version/release and platform OS?

ROW_NUMBER() OVER(...) is supported on
DB2 for LUW Version 8.2
DB2 Version 9.1 for z/OS
DB2 for i5/OS Version 5 Release 4
Reply With Quote
Reply

Tags
distinct, dummy table, self join

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On