How to eliminate Claims with ClaimStatus = 1 based on Activity Date. SQL Server 2012












1















I have 3 different claims in my dataset.



ClaimStatus can be only 1 or 0. Which indicates Open (0) or Closed (1).



How to eliminate those claims that have the last ClaimStatus = 1.



enter image description here



I tried to use Last_Value function in my WHERE clause but got an error:




Windowed functions can only appear in the SELECT or ORDER BY clauses




Query result should return only Claim2, because ClaimStatus is still open:



enter image description here



Code:



declare @TempTable table 
(
ClaimNumber varchar(50),
ActivityID int,
Activity varchar(50),
ActivityDate datetime,
ClaimStatus int
)

insert into @TempTable
values ('Claim1', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim1', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim1', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim1', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1),
('Claim2', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim2', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim2', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim3', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim3', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1)

select *
from @TempTable
where LAST_VALUE(ClaimStatus) over (partition by ClaimNumber order by ActivityDate desc) <> 1









share|improve this question




















  • 1





    Post the tables and data as text READ THIS

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:48











  • How many ClaimStatus are there?

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:50











  • Only 2 ClaimStatus. Either 1 or 0

    – Oleg
    Nov 27 '18 at 18:55






  • 1





    Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

    – Eric Brandt
    Nov 27 '18 at 19:22
















1















I have 3 different claims in my dataset.



ClaimStatus can be only 1 or 0. Which indicates Open (0) or Closed (1).



How to eliminate those claims that have the last ClaimStatus = 1.



enter image description here



I tried to use Last_Value function in my WHERE clause but got an error:




Windowed functions can only appear in the SELECT or ORDER BY clauses




Query result should return only Claim2, because ClaimStatus is still open:



enter image description here



Code:



declare @TempTable table 
(
ClaimNumber varchar(50),
ActivityID int,
Activity varchar(50),
ActivityDate datetime,
ClaimStatus int
)

insert into @TempTable
values ('Claim1', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim1', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim1', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim1', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1),
('Claim2', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim2', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim2', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim3', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim3', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1)

select *
from @TempTable
where LAST_VALUE(ClaimStatus) over (partition by ClaimNumber order by ActivityDate desc) <> 1









share|improve this question




















  • 1





    Post the tables and data as text READ THIS

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:48











  • How many ClaimStatus are there?

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:50











  • Only 2 ClaimStatus. Either 1 or 0

    – Oleg
    Nov 27 '18 at 18:55






  • 1





    Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

    – Eric Brandt
    Nov 27 '18 at 19:22














1












1








1








I have 3 different claims in my dataset.



ClaimStatus can be only 1 or 0. Which indicates Open (0) or Closed (1).



How to eliminate those claims that have the last ClaimStatus = 1.



enter image description here



I tried to use Last_Value function in my WHERE clause but got an error:




Windowed functions can only appear in the SELECT or ORDER BY clauses




Query result should return only Claim2, because ClaimStatus is still open:



enter image description here



Code:



declare @TempTable table 
(
ClaimNumber varchar(50),
ActivityID int,
Activity varchar(50),
ActivityDate datetime,
ClaimStatus int
)

insert into @TempTable
values ('Claim1', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim1', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim1', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim1', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1),
('Claim2', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim2', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim2', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim3', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim3', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1)

select *
from @TempTable
where LAST_VALUE(ClaimStatus) over (partition by ClaimNumber order by ActivityDate desc) <> 1









share|improve this question
















I have 3 different claims in my dataset.



ClaimStatus can be only 1 or 0. Which indicates Open (0) or Closed (1).



How to eliminate those claims that have the last ClaimStatus = 1.



enter image description here



I tried to use Last_Value function in my WHERE clause but got an error:




Windowed functions can only appear in the SELECT or ORDER BY clauses




Query result should return only Claim2, because ClaimStatus is still open:



enter image description here



Code:



declare @TempTable table 
(
ClaimNumber varchar(50),
ActivityID int,
Activity varchar(50),
ActivityDate datetime,
ClaimStatus int
)

insert into @TempTable
values ('Claim1', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim1', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim1', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim1', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1),
('Claim2', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim2', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim2', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 0, 'ClaimCreated', '2018-05-04 10:58:56.510', 0),
('Claim3', 4, 'ReserveCreated', '2018-05-09 09:52:52.327', 0),
('Claim3', 6, 'PaymentCreated', '2018-05-15 13:17:47.920', 0),
('Claim3', 8, 'ClaimClosed', '2018-11-01 10:53:00.087', 1)

select *
from @TempTable
where LAST_VALUE(ClaimStatus) over (partition by ClaimNumber order by ActivityDate desc) <> 1






sql-server tsql sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 20:04









marc_s

580k13011191266




580k13011191266










asked Nov 27 '18 at 18:46









OlegOleg

1,41221651




1,41221651








  • 1





    Post the tables and data as text READ THIS

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:48











  • How many ClaimStatus are there?

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:50











  • Only 2 ClaimStatus. Either 1 or 0

    – Oleg
    Nov 27 '18 at 18:55






  • 1





    Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

    – Eric Brandt
    Nov 27 '18 at 19:22














  • 1





    Post the tables and data as text READ THIS

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:48











  • How many ClaimStatus are there?

    – Juan Carlos Oropeza
    Nov 27 '18 at 18:50











  • Only 2 ClaimStatus. Either 1 or 0

    – Oleg
    Nov 27 '18 at 18:55






  • 1





    Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

    – Eric Brandt
    Nov 27 '18 at 19:22








1




1





Post the tables and data as text READ THIS

– Juan Carlos Oropeza
Nov 27 '18 at 18:48





Post the tables and data as text READ THIS

– Juan Carlos Oropeza
Nov 27 '18 at 18:48













How many ClaimStatus are there?

– Juan Carlos Oropeza
Nov 27 '18 at 18:50





How many ClaimStatus are there?

– Juan Carlos Oropeza
Nov 27 '18 at 18:50













Only 2 ClaimStatus. Either 1 or 0

– Oleg
Nov 27 '18 at 18:55





Only 2 ClaimStatus. Either 1 or 0

– Oleg
Nov 27 '18 at 18:55




1




1





Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

– Eric Brandt
Nov 27 '18 at 19:22





Can a claim be reopened? So the 1 would be somewhere other than on the most recent line?

– Eric Brandt
Nov 27 '18 at 19:22












3 Answers
3






active

oldest

votes


















2














SQL DEMO



WITH cte as (
SELECT ClaimNumber,
ClaimStatus,
ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
FROM @TempTable
)
SELECT t.*
FROM @TempTable t
JOIN (SELECT *
FROM cte
WHERE rn = 1) f
ON t.ClaimNumber = f.ClaimNumber
AND f.ClaimStatus = 0


OUTPUT



enter image description here



Additionally if only 2 status you also can do



WITH cte as (
SELECT ClaimNumber
FROM @TempTable
GROUP BY ClaimNumber
HAVING MAX(ClaimStatus) = 0
)
SELECT t.*
FROM @TempTable t
WHERE ClaimNumber IN (SELECT * FROM cte)





share|improve this answer































    1














    As the error says, you can't put LAST_VALUE in the WHERE, but you can put it in the SELECT of a CTE, and then reference that in the WHERE:



    WITH CTE AS(
    SELECT ClaimNumber,
    ActivityID,
    Activity,
    ActivityDate,
    ClaimStatus,
    LAST_VALUE(ClaimStatus) OVER (PARTITION BY ClaimNumber ORDER BY ActivityDate
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LatestStatus
    FROM @TempTable)
    SELECT ClaimNumber,
    ActivityID,
    Activity,
    ActivityDate,
    ClaimStatus
    FROM CTE
    WHERE LatestStatus != 1;





    share|improve this answer































      1














      If I'm reading your question right, it seems like what you're interested in is finding out if the last status of a claim is that it's closed. If that's correct, we just need to find the last record and then check it's status. This seems to work:



      SELECT
      tt.ClaimNumber,
      tt.ClaimStatus
      FROM
      @TempTable AS tt
      JOIN
      (
      SELECT
      t.ClaimNumber
      ,MAX(t.ActivityDate) AS LastActivity
      FROM
      @TempTable AS t
      GROUP BY
      t.ClaimNumber
      ) AS s
      ON
      s.ClaimNumber = tt.ClaimNumber
      AND s.LastActivity = tt.ActivityDate
      WHERE
      tt.ClaimStatus <> 1;


      Results:



      +-------------+-------------+
      | ClaimNumber | ClaimStatus |
      +-------------+-------------+
      | Claim2 | 0 |
      +-------------+-------------+





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506245%2fhow-to-eliminate-claims-with-claimstatus-1-based-on-activity-date-sql-server%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        SQL DEMO



        WITH cte as (
        SELECT ClaimNumber,
        ClaimStatus,
        ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
        FROM @TempTable
        )
        SELECT t.*
        FROM @TempTable t
        JOIN (SELECT *
        FROM cte
        WHERE rn = 1) f
        ON t.ClaimNumber = f.ClaimNumber
        AND f.ClaimStatus = 0


        OUTPUT



        enter image description here



        Additionally if only 2 status you also can do



        WITH cte as (
        SELECT ClaimNumber
        FROM @TempTable
        GROUP BY ClaimNumber
        HAVING MAX(ClaimStatus) = 0
        )
        SELECT t.*
        FROM @TempTable t
        WHERE ClaimNumber IN (SELECT * FROM cte)





        share|improve this answer




























          2














          SQL DEMO



          WITH cte as (
          SELECT ClaimNumber,
          ClaimStatus,
          ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
          FROM @TempTable
          )
          SELECT t.*
          FROM @TempTable t
          JOIN (SELECT *
          FROM cte
          WHERE rn = 1) f
          ON t.ClaimNumber = f.ClaimNumber
          AND f.ClaimStatus = 0


          OUTPUT



          enter image description here



          Additionally if only 2 status you also can do



          WITH cte as (
          SELECT ClaimNumber
          FROM @TempTable
          GROUP BY ClaimNumber
          HAVING MAX(ClaimStatus) = 0
          )
          SELECT t.*
          FROM @TempTable t
          WHERE ClaimNumber IN (SELECT * FROM cte)





          share|improve this answer


























            2












            2








            2







            SQL DEMO



            WITH cte as (
            SELECT ClaimNumber,
            ClaimStatus,
            ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
            FROM @TempTable
            )
            SELECT t.*
            FROM @TempTable t
            JOIN (SELECT *
            FROM cte
            WHERE rn = 1) f
            ON t.ClaimNumber = f.ClaimNumber
            AND f.ClaimStatus = 0


            OUTPUT



            enter image description here



            Additionally if only 2 status you also can do



            WITH cte as (
            SELECT ClaimNumber
            FROM @TempTable
            GROUP BY ClaimNumber
            HAVING MAX(ClaimStatus) = 0
            )
            SELECT t.*
            FROM @TempTable t
            WHERE ClaimNumber IN (SELECT * FROM cte)





            share|improve this answer













            SQL DEMO



            WITH cte as (
            SELECT ClaimNumber,
            ClaimStatus,
            ROW_NUMBER() over (PARTITION BY ClaimNumber ORDER BY ActivityDate DESC) as rn
            FROM @TempTable
            )
            SELECT t.*
            FROM @TempTable t
            JOIN (SELECT *
            FROM cte
            WHERE rn = 1) f
            ON t.ClaimNumber = f.ClaimNumber
            AND f.ClaimStatus = 0


            OUTPUT



            enter image description here



            Additionally if only 2 status you also can do



            WITH cte as (
            SELECT ClaimNumber
            FROM @TempTable
            GROUP BY ClaimNumber
            HAVING MAX(ClaimStatus) = 0
            )
            SELECT t.*
            FROM @TempTable t
            WHERE ClaimNumber IN (SELECT * FROM cte)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 18:55









            Juan Carlos OropezaJuan Carlos Oropeza

            36.8k63877




            36.8k63877

























                1














                As the error says, you can't put LAST_VALUE in the WHERE, but you can put it in the SELECT of a CTE, and then reference that in the WHERE:



                WITH CTE AS(
                SELECT ClaimNumber,
                ActivityID,
                Activity,
                ActivityDate,
                ClaimStatus,
                LAST_VALUE(ClaimStatus) OVER (PARTITION BY ClaimNumber ORDER BY ActivityDate
                ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LatestStatus
                FROM @TempTable)
                SELECT ClaimNumber,
                ActivityID,
                Activity,
                ActivityDate,
                ClaimStatus
                FROM CTE
                WHERE LatestStatus != 1;





                share|improve this answer




























                  1














                  As the error says, you can't put LAST_VALUE in the WHERE, but you can put it in the SELECT of a CTE, and then reference that in the WHERE:



                  WITH CTE AS(
                  SELECT ClaimNumber,
                  ActivityID,
                  Activity,
                  ActivityDate,
                  ClaimStatus,
                  LAST_VALUE(ClaimStatus) OVER (PARTITION BY ClaimNumber ORDER BY ActivityDate
                  ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LatestStatus
                  FROM @TempTable)
                  SELECT ClaimNumber,
                  ActivityID,
                  Activity,
                  ActivityDate,
                  ClaimStatus
                  FROM CTE
                  WHERE LatestStatus != 1;





                  share|improve this answer


























                    1












                    1








                    1







                    As the error says, you can't put LAST_VALUE in the WHERE, but you can put it in the SELECT of a CTE, and then reference that in the WHERE:



                    WITH CTE AS(
                    SELECT ClaimNumber,
                    ActivityID,
                    Activity,
                    ActivityDate,
                    ClaimStatus,
                    LAST_VALUE(ClaimStatus) OVER (PARTITION BY ClaimNumber ORDER BY ActivityDate
                    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LatestStatus
                    FROM @TempTable)
                    SELECT ClaimNumber,
                    ActivityID,
                    Activity,
                    ActivityDate,
                    ClaimStatus
                    FROM CTE
                    WHERE LatestStatus != 1;





                    share|improve this answer













                    As the error says, you can't put LAST_VALUE in the WHERE, but you can put it in the SELECT of a CTE, and then reference that in the WHERE:



                    WITH CTE AS(
                    SELECT ClaimNumber,
                    ActivityID,
                    Activity,
                    ActivityDate,
                    ClaimStatus,
                    LAST_VALUE(ClaimStatus) OVER (PARTITION BY ClaimNumber ORDER BY ActivityDate
                    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS LatestStatus
                    FROM @TempTable)
                    SELECT ClaimNumber,
                    ActivityID,
                    Activity,
                    ActivityDate,
                    ClaimStatus
                    FROM CTE
                    WHERE LatestStatus != 1;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 27 '18 at 18:57









                    LarnuLarnu

                    20.4k51732




                    20.4k51732























                        1














                        If I'm reading your question right, it seems like what you're interested in is finding out if the last status of a claim is that it's closed. If that's correct, we just need to find the last record and then check it's status. This seems to work:



                        SELECT
                        tt.ClaimNumber,
                        tt.ClaimStatus
                        FROM
                        @TempTable AS tt
                        JOIN
                        (
                        SELECT
                        t.ClaimNumber
                        ,MAX(t.ActivityDate) AS LastActivity
                        FROM
                        @TempTable AS t
                        GROUP BY
                        t.ClaimNumber
                        ) AS s
                        ON
                        s.ClaimNumber = tt.ClaimNumber
                        AND s.LastActivity = tt.ActivityDate
                        WHERE
                        tt.ClaimStatus <> 1;


                        Results:



                        +-------------+-------------+
                        | ClaimNumber | ClaimStatus |
                        +-------------+-------------+
                        | Claim2 | 0 |
                        +-------------+-------------+





                        share|improve this answer




























                          1














                          If I'm reading your question right, it seems like what you're interested in is finding out if the last status of a claim is that it's closed. If that's correct, we just need to find the last record and then check it's status. This seems to work:



                          SELECT
                          tt.ClaimNumber,
                          tt.ClaimStatus
                          FROM
                          @TempTable AS tt
                          JOIN
                          (
                          SELECT
                          t.ClaimNumber
                          ,MAX(t.ActivityDate) AS LastActivity
                          FROM
                          @TempTable AS t
                          GROUP BY
                          t.ClaimNumber
                          ) AS s
                          ON
                          s.ClaimNumber = tt.ClaimNumber
                          AND s.LastActivity = tt.ActivityDate
                          WHERE
                          tt.ClaimStatus <> 1;


                          Results:



                          +-------------+-------------+
                          | ClaimNumber | ClaimStatus |
                          +-------------+-------------+
                          | Claim2 | 0 |
                          +-------------+-------------+





                          share|improve this answer


























                            1












                            1








                            1







                            If I'm reading your question right, it seems like what you're interested in is finding out if the last status of a claim is that it's closed. If that's correct, we just need to find the last record and then check it's status. This seems to work:



                            SELECT
                            tt.ClaimNumber,
                            tt.ClaimStatus
                            FROM
                            @TempTable AS tt
                            JOIN
                            (
                            SELECT
                            t.ClaimNumber
                            ,MAX(t.ActivityDate) AS LastActivity
                            FROM
                            @TempTable AS t
                            GROUP BY
                            t.ClaimNumber
                            ) AS s
                            ON
                            s.ClaimNumber = tt.ClaimNumber
                            AND s.LastActivity = tt.ActivityDate
                            WHERE
                            tt.ClaimStatus <> 1;


                            Results:



                            +-------------+-------------+
                            | ClaimNumber | ClaimStatus |
                            +-------------+-------------+
                            | Claim2 | 0 |
                            +-------------+-------------+





                            share|improve this answer













                            If I'm reading your question right, it seems like what you're interested in is finding out if the last status of a claim is that it's closed. If that's correct, we just need to find the last record and then check it's status. This seems to work:



                            SELECT
                            tt.ClaimNumber,
                            tt.ClaimStatus
                            FROM
                            @TempTable AS tt
                            JOIN
                            (
                            SELECT
                            t.ClaimNumber
                            ,MAX(t.ActivityDate) AS LastActivity
                            FROM
                            @TempTable AS t
                            GROUP BY
                            t.ClaimNumber
                            ) AS s
                            ON
                            s.ClaimNumber = tt.ClaimNumber
                            AND s.LastActivity = tt.ActivityDate
                            WHERE
                            tt.ClaimStatus <> 1;


                            Results:



                            +-------------+-------------+
                            | ClaimNumber | ClaimStatus |
                            +-------------+-------------+
                            | Claim2 | 0 |
                            +-------------+-------------+






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 27 '18 at 20:49









                            Eric BrandtEric Brandt

                            2,76511025




                            2,76511025






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53506245%2fhow-to-eliminate-claims-with-claimstatus-1-based-on-activity-date-sql-server%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Contact image not getting when fetch all contact list from iPhone by CNContact

                                count number of partitions of a set with n elements into k subsets

                                A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks