Fuzzy matching by using SimMetrics library - fuzzy-search

I need some help here. How would, I create a simple SQL statement to select Names #userEnteredName with these functions. In other words, I want to get customer names from the customer table where the user typed in smyth and get back smith, smitty, etc....
... or with one word How do I use the bellow created functions to query a database table.
Thank you in advance for you help.
<code>declare #userEnteredLastName varchar(200);
declare #userEnteredFirstName varchar(200);
set #userEnteredLastName='smyth';
set #userEnteredFirstName='Jon';
SELECT * FROM Customer WHERE JaroWinkler(CustomerLastName, #userEnteredLastName) > .75 AND JaroWinkler(CustomerFirstName, #userEnteredFirstName) > .75</code>
I using the SimMetrics library for it located at SimMetrics

If you are using SQL Server 2008 and above (should work on 2005, I have not tested it)...
Try using a Table Value Function (TVF) in the form of CROSS APPLY to make this work.
You can wrap the scalar function in a TVF to accomplish this, like so:
CREATE FUNCTION dbo.Jarowinklertvf(#firstword NVARCHAR(255),
#secondword NVARCHAR(255))
returns TABLE
AS
RETURN
(SELECT dbo.Jarowinkler(#firstword, #secondword) Score,
'JaroWinkler' Metric)
Then call the function like so:
DECLARE #userEnteredLastName VARCHAR(200);
DECLARE #userEnteredFirstName VARCHAR(200);
SET #userEnteredLastName='smyth';
SET #userEnteredFirstName='Jon';
SELECT l.score LastNameScore,
f.score FirstNameScore,
i.*
FROM customer i
CROSS apply dbo.Jarowinklertvf(i.customerlastname, #userEnteredLastName)
l
CROSS apply dbo.Jarowinklertvf(i.customerfirstname, #userEnteredFirstName
) f
WHERE l.score > .75
AND f.score > .75
ORDER BY 1 DESC
I hope this helps.

Related

Oracle query in stored procedure

i'm working with stored procedures, i have a query that should bring one specific id
SELECT * into SID_INCOMING FROM
(
SELECT SID
FROM TBL_INCOMING
WHERE XKEY = ''||nro_tarjetatmp||'_'||fecha_hr_trasacfinal||'_'||datos_referencia_adquirentetmp||''
AND CODIGO_AUTORIZACION IN(''||codigo_autorizacion||'')
AND MIT IN(''||mit||'')
ORDER BY SID ASC
) WHERE ROWNUM <= 1;
the variables values are in order
4946110112060005_200116_74064350165099586691985
536018
05
when it's executed i get this result
but when i execute the same query with the same parameters i get this result
and this is the one i sould get with the procedure, so why is this happening?
it seems to me, that the sp is not considering the second and third parameter
any help is welcome, thanks
You need to use aliases to avoid your problem with AND MIT IN(''||mit||''): this predicate compares column MIT with itself.
SO just add aliases:
SELECT * into SID_INCOMING FROM
(
SELECT SID
FROM TBL_INCOMING ti
WHERE ti.XKEY = ''||your_proc_name.nro_tarjetatmp||'_'||your_proc_name.fecha_hr_trasacfinal||'_'||your_proc_name.datos_referencia_adquirentetmp||''
AND ti.CODIGO_AUTORIZACION IN(''||your_proc_name.codigo_autorizacion||'')
AND ti.MIT IN(''||your_proc_name.mit||'')
ORDER BY ti.SID ASC
) WHERE ROWNUM <= 1;
Also why do you add ''||? It doesn't add anything into your variables, but forces impicit type conversion in cases when they are not varchar2/char data types.
Could you show the results of "describe TBL_INCOMING" please? Since looks like CODIGO_AUTORIZACION is a number data type and probably MIT is number too.

How to Have Function Definition and Subquery in With Clause of Oracle Select Statement?

I know the right syntax for having a function definition in the WITH clause. I know the right syntax for having a subquery in the WITH clause. But I have been unable to find an example of having a subquery and a function definition in the WITH clause of a SELECT statement.
If I have:
with totals as ( select colum_name from some_table )
select sum(column_name) from totals;
How do I add a function definition in the WITH clause?
Since you can't find much/anything about this from Oracle, I don't think it is a good idea to use it. Anyway, this works in 18.1:
WITH
FUNCTION with_plus(p IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN p + 1;
END;
FUNCTION with_min(p IN NUMBER) RETURN NUMBER IS
BEGIN
RETURN p - 1;
END;
qry1 AS (
SELECT with_plus(10) plus
FROM DUAL
),
qry2 AS (
SELECT plus, with_min(10) min
FROM qry1
)
SELECT *
FROM qry2
;
/
So don't forget the slash / at the end.
If you ever find out how to put this whole block in a subquery, please let me know
I don't think there's any such restriction. However, I suspect that your problem has to do with column aliasing. Here's what worked for me:
with totals as (select sum(column_name) c1 from some_table)
select c1 from totals;
Oracle might have complained because you were trying to do something like:
with totals as (select sum(column_name) from some_table)
select sum(column_name) from totals;
Unfortunately, this is a consequence of name resolution. The subquery's column will get named "sum(column_name)". Since sum is a function, there's no way to reference that column name without Oracle thinking you're referencing the function. You have to give it another name in order to reference it anywhere else.
Edit: It seems that you want to define a function as if you would a view subquery. I don't think anything like this is possible. View subqueries really only perform textual substitution.
PL/SQL functions require a whole different parser, name resolution, compilation process, etc. Having them work in queries alone is hard enough.
Sorry to say, but you'd have to define your packages/procedures/functions normally.

if Statement SAP HANA Views

Add following filter on a column in SAP HANA Analytical view using if statement
if(Col1='a') col2=Col2
else if(Col2='b') col2=col2*1
Can someone help to give me syntax for HANA IF statement for following logic?
Why not using the documentation at the first place?
Not really clear what you are trying to do here. Look's like you are calculating something using col2 based on comparison on col1. As View will not allow you to update the value in the column, you will need to create col3 and put there the following:
if("Col1" = 'a',"Col2", if("Col1" = 'b',"Col2" * 1,'not a not b') )
BTW, do you think col2=col2*1 makes any sense?
Is it possible that you (or Shidai) are confusing the IF-Function with the IF-Statement? Both are working differently:
SELECT IF("Col1"=='a', 'aaahhh', 'uhhhhh') FROM DUMMY;
This works just like in an Excel: If Col1 is 'a' then the first value is returned, otherwise the second.
DECLARE x VARCHAR(100);
IF "Col1"='a'
THEN
x := "Col2";
ELSEIF "Col2"='b'
THEN
x := "Col2" * 1;
END IF
This is a control structure and only allowed in a SQLScript block, e.g. a stored procedure or anonymous block. You cannot use it in a simple SELECT statement.
It's not so clear what you are trying to do with assigning to col2, so I used x instead.
Also note:
HANA is case-sensitive. If you want to use the column Col1 you must write "Col1".
There is also CASE, which works similar to the IF-Function.

Oracle: How to create a function returning values for a "SELECT * FROM tab WHERE name IN (function())"

I have a problem which I can't solve. Maybe you have an idea about how to solve it.
I do have a given parameter-table like this:
P_VALUE P_NAME
----------- ----------
X85 A_03
XH1 A_04
XH2 A_04
XH3 A_04
C84 A_05
As you can see there are parameters with multiple entries. At the moment this parameters are used in this way:
SELECT * FROM tablex
WHERE code IN (SELECT p_value
FROM parameter_table
WHERE p_name LIKE 'A_04');
As the query is very big these parameter sub-select are used very often. I was trying to implement a function in Oracle to get my parameters. This works very fine as long as there is just 1 row per parameter. When I want to use it in "IN-Statements", it won't work because functions just return a single value.
--WORKS
SELECT * FROM tablex
WHERE code = (f_get_param('A_03'));
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
Please note that I need it for plain SQL statements, so procedures won't work as they are just good for PL/SQL.
I would be really thankful for good ideas or help!
Use collections. Here you have an example http://www.adp-gmbh.ch/ora/plsql/coll/return_table.html
Technically you can achieve using the function this way but doing this will cause index not to be used on code column on tablex and may affect performance .Using function index you can reduce performance impact
CREATE OR REPLACE FUNCTION f_get_param(p_value1 IN VARCHAR2,p_name1 in VARCHAR2) return NUMBER
DETERMINISTIC
IS
l_count NUMBER;
BEGIN
select count(1) into l_count from parameter_table where p_value =p_value1
and p_name=p_name1;
if l_count > 0
then
return 1;
else
return 0;
end if;
end f_get_param;
AND use the select statement like this
SELECT * FROM tablex
WHERE f_get_param(code,'A_04')=1;
EDIT 1:-
Also to reduce the performance impact in database 10.2 and greater If the parameter_table is static you can use the DETERMINISTIC clause in the Function to say that the function returns the same value if called with same parameters every time
Please find the link on the article about using functions in SELECT statement
--DOES NOT WORK
SELECT * FROM tablex
WHERE code IN (f_get_param('A_04'));
-- Try this
SELECT * FROM tablex
WHERE code IN (select * from TABLE(f_get_param('A_04')));
You have to "CAST" a collection onto SQL TABLE.
Also when you use cast you can also use inner joint:
SELECT * FROM tablex join TABLE(f_get_param('A_04') using (code);
I think - generally - your problem is called "Dynamic where clause". Try to search some articles about it on AskTom.
I think the actual solution to your problem is to simply join the two tables and create the appropriate indexes rather than invoking a PL/SQL function at all:
SELECT x.* FROM tablex x, parameter_table p
WHERE x.code = p.p_value
AND p.p_name LIKE '%A_04%';
Note that you also have a semantic error in your LIKE clause. You're not using the % sign therefore your LIKE 'A_04' is just the same as = 'A_04'

Functional Where-In Clause - Oracle PL/SQL

I've got an association table that groups accounts together.
I'm trying to select a subset of table 'target'
p_group_id := 7;
select *
target t
where t.account_id in get_account_ids(p_group_id);
Is it possible to write a function that returns a list of account_ids (as some form of collection) that would facilitate the above code?
I've looked at pipelined functions, however I want to stay away from loops and cursors. Also, custom types / table also get into casting that I'd like to avoid.
For reference, here's some pseudocode for what the function 'get_account_ids' would do, hypothetically:
function get_account_ids(p_group_id)
insert into v_ret
select aa.account_id
from assoc_account aa
where aa.groupid = p_group_id;
return v_ret;
You simply need:
select *
from target t
where t.account_id in
( select aa.account_id
from assoc_account aa
where aa.groupid = 7;
)
The above will work, assuming that assoc_account.account_id is never NULL.

Resources