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 > Selecting distinct columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-20-09, 07:47
mka12 mka12 is offline
Registered User
 
Join Date: Jan 2009
Posts: 11
Selecting distinct columns

Hi,

I'm using simple select and insert from one database to another using federated objects.

Database X
The table from which I'm selecting has
col1 + col2 = primary key

Database Y
Table in which I'm inserting is
col1 + col3 = primary key

Since col2 is no longer used in database Y
How can I select distinct values from database X on col1 + col3 along with other columns

Please let me know if I need to explain it with example.
Reply With Quote
  #2 (permalink)  
Old 04-20-09, 08:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If col1 + col3 in the old table are unique values, then you are home free. If they are not then only you can determine which rows from the old table are to be inserted into the new table.

Andy
Reply With Quote
  #3 (permalink)  
Old 04-21-09, 00:45
mka12 mka12 is offline
Registered User
 
Join Date: Jan 2009
Posts: 11
Combination of col1+col3 should be unique as col3 is a timestamp.
But my concern is how do I from a query something like:
select distinct (col1+col3) , col4,col5,col6.
As above syntax for distinct is not correct , so distinct will work on all columns
Reply With Quote
  #4 (permalink)  
Old 04-21-09, 08:20
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If col1+col3 is unique, then col1,col3,col4,col5,etc,etc will also be unique so a straight select should do it

insert into Y select col1,col3,.... from X

Andy
Reply With Quote
  #5 (permalink)  
Old 04-22-09, 23:13
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
But my concern is how do I from a query something like:
select distinct (col1+col3) , col4,col5,col6.
Quote:
If col1+col3 is unique, then col1,col3,col4,col5,etc,etc will also be unique so a straight select should do it
If (col1+col3) is not unique and
1) col4, col5, col6 are not neccesary selected from same row.
Code:
SELECT col1, col3, MAX(col4), MAX(col5), MAX(col6)
  FROM tableX
 GROUP BY col1, col3;
2) col4, col5, col6 should be selected from same row.
Code:
SELECT col1, col3, col4, col5, col6
  FROM
     ( SELECT col1, col3, col4, col5, col6
            , ROW_NUMBER() OVER(PARTITION BY col1, col3) rn
         FROM tableX
     ) AS x
 WHERE rn = 1;
Reply With Quote
Reply

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