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 > MySQL > Fill Left Outer Join Nulls with previous value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-12-10, 11:40
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Fill Left Outer Join Nulls with previous value

I was curious if it was possible to fill Left Outer Join Nulls with the previous value.


e.g.


The left outer join returns:

ColA ID A B
Apple 1 Apple 2
Apple 2 Apple 10
Banana NULL NULL NULL
Orange 3 Orange 3
Pear 4 Pear 16


while I'd like to have

ColA ID A B
Apple 1 Apple 2
Apple 2 Apple 10
Banana 2 Apple 10
Orange 3 Orange 3
Pear 4 Pear 16



CREATE TABLE IF NOT EXISTS `Table1` (
`A` varchar(25) NOT NULL,
PRIMARY KEY (`A`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `Table1` (`A`) VALUES
('Apple'),
('Banana'),
('Orange'),
('Pear');

CREATE TABLE IF NOT EXISTS `Table2` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`A` varchar(25) NOT NULL,
`B` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

INSERT INTO `Table2` (`ID`, `A`, `B`) VALUES
(1, 'Apple', 2),
(2, 'Apple', 10),
(3, 'Orange', 3),
(4, 'Pear', 16);

SELECT *
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.A = Table2.A;


Thanks!
Reply With Quote
  #2 (permalink)  
Old 07-12-10, 11:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by Diesel Dud View Post
I was curious if it was possible to fill Left Outer Join Nulls with the previous value.
yes, if you would be so kind as to provide the means by which the previous row can be detected

remember, the rows in a table have no sequence
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-12-10, 12:21
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Quote:
Originally Posted by r937 View Post
yes, if you would be so kind as to provide the means by which the previous row can be detected

remember, the rows in a table have no sequence
previous row would be identified using Table2.ID
e.g. ID=1 is ID=2's previous row
Thanks
Reply With Quote
  #4 (permalink)  
Old 07-12-10, 12:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
no, that doesn't work

Banana has no match, so how do you know what the "previous" match is?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-12-10, 12:32
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Quote:
Originally Posted by r937 View Post
no, that doesn't work

Banana has no match, so how do you know what the "previous" match is?
I see what you mean.

Hypothetically, if I were to insert the LEFT OUTER JOIN result (in the order I require) in a workfile that has an auto-increment ID as primary key, I could update NULL values with previous values, right?
Reply With Quote
  #6 (permalink)  
Old 07-12-10, 12:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
i don't think i can offer any further advice except to say you should consider doing this in your application code and not in the database, temp tables or otherwise

the reason i can't give more solid advice is because we really aren't talking about apples and bananas, and between the two of us, only one knows what it's really all about
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-12-10, 13:02
Diesel Dud Diesel Dud is offline
Registered User
 
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
Quote:
Originally Posted by r937 View Post
i don't think i can offer any further advice except to say you should consider doing this in your application code and not in the database, temp tables or otherwise

the reason i can't give more solid advice is because we really aren't talking about apples and bananas, and between the two of us, only one knows what it's really all about
Thanks a lot, I appreciate your help.
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