1

I am a beginner in SQL and I don't know how to solve this question. How to combine the following SQL query and the stored procedure into a single SQL query. This is a PostgreSQL query and function. Any help is appreciated.

The following query calls a stored procedure/function for each value of the query:

 SELECT t.trj_id, T2.trj_id, Similar_trj_woffset(t.trj_id, T2.trj_id) 
 FROM   transitions T1, 
        transitions T2 
 WHERE  T1.w_s = T2.w_s 
 AND T1.w_e = T2.w_e 
 AND T1.trans_id <> T2.trans_id 

The stored procedure/function is:

create or replace function similar_trj_woffset(
  IN t1 as integer,
  IN t2 as integer,
  OUT score as integer,
  OUT offset as integer
) AS $$
declare 
off_set integer :=0;
score  integer := 0;
cou    integer;
time1  integer;
time2  integer;
dist   integer;
begin
for off_set in 0..10 LOOP
       time1 := 0;
       time2 := 0 + off_set;
       cou := 0;
       while time2 < 100 Loop 
          select vec_length(P.x-P2.x,P.y-P2.y,P.z-P2.z) into dist
          from AtomPositions P, AtomPositions P2
          where P.trj_id  = t1
            and   P2.trj_id = t2
            and   P.t = time1
            and   P2.t = time2;
        if dist < cuto`enter code here`ff then
            cou : = cou + 1;
        time1 := time1 + 1;
        time2 := time2 + 1;
    end loop
    if cou > score then
       score := cou ;
       offset := off_set;
    end if;
    off_set := off_set + 1;
end loop;

end $$ language plpgsql;

Can some one tell me how to merge the query and the stored procedure into one single SQL query.

1
  • I want to merge it for query optimization. Commented Mar 10, 2013 at 2:23

2 Answers 2

1

To call this function an split the returned record into individual columns:

SELECT t.trj_id, t2.trj_id, (similar_trj_woffset(t.trj_id, t2.trj_id)).*
FROM   transitions t1
JOIN   transitions t2 USING (w_s, w_e)
WHERE  t1.trans_id <> t2.trans_id 

Note the parenthesis around the function call.
I also rewrote the query to use proper ANSI JOIN syntax with a simplified equijoin condition (USING) and removed the spurious upper-casing.

Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't merge them is your basic answer. One purpose of the code being in a function is so it can be called from multiple queries and be maintained in one place. Merging the two doesn't make sense when the query is already calling the function on each iteration.

What are you expecting to gain from doing this because simply moving the code isn't going to do anything positive?

Actually, if any other queries use it, they will break if you remove it.

1 Comment

Only this query uses this function and I want to merge them for the sake of query optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.