MySQL Forums
Forum List  »  Newbie

Re: VLOOKUP in sql
Posted by: Vinod Sugur
Date: October 08, 2008 12:17AM

Hi,

I converted the xls sheet into table having following structure:

CREATE TABLE `table_1` (
`No` decimal(10,0) unsigned NOT NULL,
`C` varchar(7) NOT NULL,
`D` varchar(7) NOT NULL,
`E` varchar(7) NOT NULL,
`F` varchar(7) NOT NULL,
`G` varchar(7) NOT NULL,
`H` varchar(7) NOT NULL,
PRIMARY KEY (`No`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I can get the result of following vlookup formula from the below queries:

a. VLOOKUP(869 ; "table" ; 3)
the end result is 4% (gets the value in the line below 870 (line: 770) and the column 3 of the table)

=======================
Select value
from
(select No, col,IF(col=1,C, IF(col=2,D, IF(col=3,E,IF(col=4,F,IF(col=5,G,IF(col=6,H,NULL)))))) value
from table_1, (Select 1 col
UNION
Select 2
UNION
Select 3
UNION
Select 4
UNION
SELECT 5
UNION
SELECT 6) 6_colunmns) vlookup

where no <= 869 /* first parameter of vlookup */
and col = 2 /* third parameter of vlookup -1 */
order by no desc limit 1
=======================

b. VLOOKUP(871 ; "table" ; 4)
the end result is 3% (gets the value in the line below 950 (line: 870) and the column 4 of the table)

=======================
Select value
from
(select No, col,IF(col=1,C, IF(col=2,D, IF(col=3,E,IF(col=4,F,IF(col=5,G,IF(col=6,H,NULL)))))) value
from table_1, (Select 1 col
UNION
Select 2
UNION
Select 3
UNION
Select 4
UNION
SELECT 5
UNION
SELECT 6) 6_colunmns) vlookup
where no <= 871 /* first parameter of vlookup */
and col = 3 /* third parameter of vlookup -1 */
order by no desc limit 1
=======================

Let me know if this works.

~Vinod

Options: ReplyQuote


Subject
Written By
Posted
October 07, 2008 11:07AM
October 07, 2008 04:11PM
October 08, 2008 03:11AM
October 08, 2008 03:23AM
Re: VLOOKUP in sql
October 08, 2008 12:17AM
October 08, 2008 03:10AM
October 08, 2008 08:15AM
October 08, 2008 08:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.