SQL 2005 Procedure

In the Select statement I have joined a new Table Claims_Ceded_Amounts. What I want to do is if the ClaimNo from wellandp.ClaimFinancial or Table 1 is in Claims_Ceded_Amounts or Table 2 then the SUM(ExpPaid) AS ExpPaid should come from table 2 column ExpPaidCeded ELSE it should be ExpPaid from Table 1
( SELECT    ClaimNo
                                                   ,SUM(IndemResAmt) AS IndemResAmt
                                                   ,SUM(IndemPaid) AS IndemPaid
                                                   ,SUM(ExpResAmt) AS ExpResAmt
                                                   ,SUM(ExpPaid) AS ExpPaid                                                   
                                          FROM      wellandp.ClaimFinancial
                                          LEFT OUTER JOIN dbo.Claims_Ceded_Amounts CCA ON wellandp.ClaimFinancial.ClaimNo = CCA.ClaimNoCeded
                                          WHERE     wellandp.ClaimFinancial.TypeOfBusiness = 'T'
                                                    AND wellandp.ClaimFinancial.InitialAcctgDate <= DATEADD(day,
                                                              -1,
                                                              DATEADD(mm,
                                                              DATEDIFF(m, 0,
                                                              @AsOfMonth
                                                              + '/1/'
                                                              + @AsOfYear) + 1,
                                                              0))                                                               
                                          GROUP BY  ClaimNo
                                        ) AS CededFile ON wellandp.ClaimGeneral.ClaimNo = CededFile.ClaimNo

Open in new window

mburk1968Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TempDBACommented:
(
    SELECT ClaimNo,
           SUM(IndemResAmt) AS IndemResAmt,
           SUM(IndemPaid) AS IndemPaid,
           SUM(ExpResAmt) AS ExpResAmt,
           ExpPaid= ISNULL(SUM(CCA.ExpPaid),SUM(wellandp.ClaimFinancial.ExpPaid))
    FROM   wellandp.ClaimFinancial
           LEFT OUTER JOIN dbo.Claims_Ceded_Amounts CCA
                ON  wellandp.ClaimFinancial.ClaimNo = CCA.ClaimNoCeded
    WHERE  wellandp.ClaimFinancial.TypeOfBusiness = 'T'
           AND wellandp.ClaimFinancial.InitialAcctgDate <= DATEADD(
                   DAY,
                   -1,
                   DATEADD(mm, DATEDIFF(m, 0, @AsOfMonth + '/1/' + @AsOfYear) + 1, 0)
               )
    GROUP BY
           wellandp.ClaimFinancial.ClaimNo
) AS CededFile ON wellandp.ClaimGeneral.ClaimNo = CededFile.ClaimNo
0
_agx_Commented:
If the ClaimNo doesn't match, the expense column from table2 will be NULL. So a simple IsNull() should do it. If not, you could use a CASE instead.

    ... SUM(  ISNULL(CCA.ExpPaidCeded , wellandp.ExpPaid ) ) AS ExpPaid


0
mburk1968Author Commented:
Perhaps I didn't explain myself properly.
There is always a match for ClaimNo and an amount for ExpPaid in table 1 or wellandp.ClaimGeneral

What I want is if ClaimNo exist in Table 2 dbo.Claims_Ceded_Amounts then disregard the amount in table 1 and sum ExpPaidCeded As ExpPaid Else ExpPaid from Table 1
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

_agx_Commented:
That should be exactly what my example does.  

Maybe my explanation was unclear.  If thereis  a matching ClaimNo in table2 then "ExpPaidCeded" will be used. Otherwise, the "ExpPaid" value from table1 is used.
0
mburk1968Author Commented:
I think I was expecting a Case statement as down the road there might be additional criteria that I will need to compare against to include a possible date range.
0
_agx_Commented:
Sure you could use a CASE.  Right now it does the same thing as ISNULL(...), but you can add conditions as needed.

... SUM(  
            CASE WHEN CCA.ClaimNoCeded IS NULL THEN wellandp.ExpPaid
                     ELSE CCA.ExpPaidCeded
            END
         ) AS ExpPaid

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.