| |
|
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.
|
 |

07-12-10, 11:40
|
|
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!
|
|

07-12-10, 11:58
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
|
|
Quote:
Originally Posted by Diesel Dud
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
|
|

07-12-10, 12:21
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
|
|
Quote:
Originally Posted by r937
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
|
|

07-12-10, 12:25
|
|
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?
|
|

07-12-10, 12:32
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
Quote:
Originally Posted by r937
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?
|
|

07-12-10, 12:57
|
|
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
|
|

07-12-10, 13:02
|
|
Registered User
|
|
Join Date: Feb 2010
Location: Montreal, CA
Posts: 32
|
|
Quote:
Originally Posted by r937
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.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|