0

I am new to SQL server, can you please help me with the query below query : I have below Table - Table1 :-

PostingDate     ReturnCheckReason           PaymentStatus   PolicyNumber
7/23/2020 15:30 Cancel Payment                  Return          1234
8/6/2020 17:40  Cancel Payment                  Return          1234

Here I would to display return response as, this is customized response

PostingDate     ReturnCheckReason           PaymentStatus           PolicyNumber
null                Cancel Payment      ALL Payments are Canceled           1234

This is My query:-

 SELECT TOP 1
    [PolicyNumber],
    [PostingDate],
    [PaymentStatus]
    FROM [dbo].[Bil_PaymentSearch] WITH (NOLOCK)
    WHERE 
         (PolicyNumber = @PolicyNumber) AND
          (REturnCheckreason <> 'Cancel payment') AND 
         (PaymentOrReturn <> 'Return')
    ORDER BY PostingDate ASC

If the Table Data like this :-

PostingDate     ReturnCheckReason   PaymentStatus   PolicyNumber
7/23/2020 15:30 Null                    Payment         1234
8/6/2020 17:40  Null                    Payment         1234
8/4/2020 14:29  Null                    Payment         1234
8/5/2020 6:09   Null                    Payment         1234
8/5/2020 12:47  Cancel Payment          Return          1234 

Then I need to return If the first payment is not cancelled , I need return that row. So I wrote this query -

 SELECT TOP 1
    [PolicyNumber],
    [PostingDate],
    [PaymentStatus]
    FROM [dbo].[Bil_PaymentSearch] WITH (NOLOCK)
    WHERE 
         (PolicyNumber = @PolicyNumber) AND
          (REturnCheckreason <> 'Cancel payment') AND 
         (PaymentOrReturn <> 'Return')
    ORDER BY PostingDate ASC 

So Now I need to combine your query and my query to handle both the below scenario, can you please help me

  1. If the first payment is not cancelled , I need return that row.
  2. If all the payments are cancelled for that policy as shown above, then we need to display customized message , or else my query will display NULL

1 Answer 1

2

You can use aggregation and conditional logic:

SELECT PolicyNumber, MAX(ReturnCheckReason), 
       (CASE WHEN MIN(ReturnCheckReason) = MAX(ReturnCheckReason) AND MAX(ReturnCheckReason) = 'Cancel payment'
             THEN 'All Payments Cancelled'
         END)
FROM [dbo].[Bil_PaymentSearch] bps
WHERE PolicyNumber = @PolicyNumber 
GROUP BY PolicyNumber
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Gordon, Thanks for your quick response, I am sorry to say that , there is little bit modification in my question, can you please help me
Wouldn't MAX(ReturnCheckReason) always equal MAX(ReturnCheckReason) ?
Can you please help me John or Gordon
Hi Gordon, I tried to solve using below Query, kindly let me know if it is any other better way I can able to solve :-

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.