1

I have a SQL query where in I have to fetch an additional param from a different table, only if a value is present in the primary table. I have tried using the if else clause, but it never works.

The SQL looks like this:

SELECT  
   hist.I_PAYT_REF AS PAY_REF,
   hist.C_USERID AS USER_ID,
   IF(@hist.I_PAYT_REF IS NULL)
   BEGIN
      SELECT 
         PAYT.I_PAYT_REQ_SUM AS PAYMENT_FILE_ID
      Join 
         SP.TSP_CP_PAYT_RQ_DTL PAYT ON PAYT.I_PAYT_REF = hist.I_PAYT_REF
   END
FROM 
   sp.TSP_CP_HIST hist
WHERE 
   hist.C_HIST_TYPE ='V'

All I want to do is fetch a a new parameter if the hist.I_PAYT_REF is not null. Any help will be greatly appreciated.

Thanks, Yeshwanth

2
  • SQL is just the Structured Query Language - a language used by many database systems, but not a a database product... many things are vendor-specific - so we really need to know what database system (and which version) you're using.... Commented Oct 27, 2012 at 15:05
  • @ marc_s: I apologize for not providing the complete details. I will get the details and update the post at the earliest. Commented Oct 27, 2012 at 15:45

3 Answers 3

2

You just need a left join, if I_PAYT_REF is null the join will not exclude the main hist record and PAYMENT_FILE_ID will be returned null anyway.

SELECT  
    hist.I_PAYT_REF AS PAY_REF,
    hist.C_USERID AS USER_ID,
    PAYT.I_PAYT_REQ_SUM AS PAYMENT_FILE_ID
    FROM 
         SP.TSP_CP_HIST hist left join 
         SP.TSP_CP_PAYT_RQ_DTL PAYT ON PAYT.I_PAYT_REF = hist.I_PAYT_REF 
    WHERE hist.C_HIST_TYPE ='V'
Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer that worked for me and that too without if condition which I am not familiar with. But the other suggestions worked too :)
2

sometimes you may be need that kind of inline if, and it's a CASE clause, it works like inline if :)

select
    hist.I_PAYT_REF as PAY_REF,
    hist.C_USERID as USER_ID,
    case
        when hist.I_PAYT_REF is null then PAYT.I_PAYT_REQ_SUM
        else null
    end as PAYMENT_FILE_ID
from sp.TSP_CP_HIST as hist
    left outer join SP.TSP_CP_PAYT_RQ_DTL as PAYT on PAYT.I_PAYT_REF = hist.I_PAYT_REF
where hist.C_HIST_TYPE ='V'

but as @jenson-button-event says - there's no point to use case here - if I_PAYT_REF is null then left outer join return no records from SP.TSP_CP_PAYT_RQ_DTL. So you can write just

select
    hist.I_PAYT_REF as PAY_REF,
    hist.C_USERID as USER_ID,
    PAYT.I_PAYT_REQ_SUM
from sp.TSP_CP_HIST as hist
    left outer join SP.TSP_CP_PAYT_RQ_DTL as PAYT on PAYT.I_PAYT_REF = hist.I_PAYT_REF
where hist.C_HIST_TYPE ='V'

1 Comment

your 'case' is redundant see my answer
0

Join both tables and apply CASE statement on your select clause.

SELECT  
    hist.I_PAYT_REF AS PAY_REF,
    hist.C_USERID AS USER_ID,
    CASE WHEN hist.I_PAYT_REF IS NULL
        THEN PAYT.I_PAYT_REQ_SUM
        ELSE I_PAYT_REF
    END as PAYMENT_FILE_ID
FROM    sp.TSP_CP_HIST hist
        INNER JOIN SP.TSP_CP_PAYT_RQ_DTL AS PAYT
              ON PAYT.I_PAYT_REF = hist.I_PAYT_REF
WHERE hist.C_HIST_TYPE ='V'

2 Comments

I think it's better to use left outer join instead of inner join or you can miss some data, but it depends on schema
also you have two ON keywords in the JOIN clause

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.