I have a table with the following values
ACCNO TEXT
------------- ----------------
8611004050001 internal payment
861100405000X external payment
8611XXXXXXXXX other payments
suppose if the accono is 8611004050002 then i have to pic external payment. if the accno is 8611211216223 then i have to get other payments.
I need to get the nearest matching value is single sql I have tried with case statement its not working. what is the efficient method to achieve this.
Pleas help me
Hmmm, one method uses a giant case
statement. Something like this:
select t.*
from t
order by (case when accno = <accno> then 99
when left(accno, 15) = left(<accno>, 15) then 15
when left(accno, 14) = left(<accno>, 14) then 14
when left(accno, 13) = left(<accno>, 13) then 13
when left(accno, 12) = left(<accno>, 12) then 12
when left(accno, 11) = left(<accno>, 11) then 11
when left(accno, 10) = left(<accno>, 10) then 10
when left(accno, 9) = left(<accno>, 9) then 9
when left(accno, 8) = left(<accno>, 8) then 8
when left(accno, 7) = left(<accno>, 7) then 7
when left(accno, 6) = left(<accno>, 6) then 6
when left(accno, 5) = left(<accno>, 5) then 5
when left(accno, 4) = left(<accno>, 4) then 4
when left(accno, 3) = left(<accno>, 3) then 3
when left(accno, 2) = left(<accno>, 2) then 2
when left(accno, 1) = left(<accno>, 1) then 1
else 0
end) desc
fetch first 1 row only;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.