How to use CROSS APPLY in this scenario

LCJ

I have a ProductStatus table as listed below. I need to list all products whose latest status is “SU”. Along with that I need to list what was the previous status of this product.

Based on referring various posts, it seems like CROSS APPLY will be suitable for this. I made an attempt as listed below but that didn’t give the expected result.

What is the best way to achieve this in SQL Server 2005 (without using subquery)?

DECLARE @ProductStatus TABLE (ProductStatusID INT,  productCode VARCHAR(5), statusCode VARCHAR(2))

INSERT INTO @ProductStatus

SELECT 1,'10011','RE'  --Recevied
UNION
SELECT 2,'10011','SU'  --Suspended

UNION
SELECT 3,'10012','IT'  -- In Transit
UNION
SELECT 4,'10012','RE' -- Received
UNION
SELECT 10,'10012','PR' -- Produced
UNION
SELECT 12,'10012','SU'  -- Suspended

UNION
SELECT 14,'10013','RE'  -- Recevied

UNION
SELECT 16,'10014','SU'  -- Recevied
UNION
SELECT 18,'10014','RE'  -- Recevied

CROSS APPLY attempt

SELECT *
FROM @ProductStatus P
CROSS APPLY 
(
   SELECT MAX(V.ProductStatusID) as maxVal
   FROM @ProductStatus V
   WHERE V.ProductCode = P.ProductCode
   AND V. ProductStatusID <  P.ProductStatusID 
   GROUP BY V.ProductCode   
)ML
WHERE P.statusCode = 'SU'

EXPECTED RESULT

enter image description here

Gordon Linoff

You can do this with cross apply but I think row_number() is an easier approach:

select ProductCode,
       max(case when seqnum = 1 then statusCode end) as LastStatus,
       max(case when seqnum = 2 then statusCode end) as PrevStatus
from (select p.*,
             row_number() over (partition by ProductCode order by ProductStatusId desc) as seqnum
      from @ProductStatus p
     ) p
group by ProductCode
having max(case when seqnum = 1 then statusCode end) = 'SU';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use a SQL Cross Apply and a MERGE

From Dev

How to use a SQL Cross Apply and a MERGE

From Dev

Complex rolling scenario (CROSS APPLY and OUTER APPLY example)

From Dev

How can I use Cross Apply to multiple rows?

From Dev

How to apply lazy quantifier in this given scenario?

From Dev

How to apply conditional aggregation in SQL the following scenario?

From Dev

How to use collect() in drools in this scenario

From Dev

How to apply CROSS JOIN in PostgreSQL?

From Dev

Sharepoint: How to show AppendOnlyHistory on a display template in a cross-publishing scenario

From Dev

how to use insert to update columns from another table with user defined function cross apply

From Dev

How to use RethinkDB indices in the following scenario?

From Dev

How would I use VCR (with WebMock) in this scenario?

From Dev

How to use threading or Task pattern in following scenario

From Dev

How to use 2D vector in this scenario?

From Dev

How to use expandable list view in the following scenario

From Dev

How to use threading or Task pattern in following scenario

From Dev

How to use bluebird promise for specific scenario

From Dev

Confused in how to apply foreign key concept in my scenario

From Java

When should I use cross apply over inner join?

From Dev

How to use scons for cross build

From Dev

How to use cross validation in MATLAB

From Dev

How to use variable extracted in JSON path extractor for further steps of scenario?

From Dev

Xcode 5, how to use autolayout with orientation, in the shown scenario?

From Dev

How to use create event on a jQuery dialog in following scenario?

From Dev

How do I use unique_ptr in this scenario?

From Dev

How can I use lambdaj groups for default case scenario

From Dev

How to use saved variable values outside of gatling scenario in scala file

From Dev

How to access json data and use in if condition in following scenario?

From Dev

How to use variable extracted in JSON path extractor for further steps of scenario?

Related Related

  1. 1

    How to use a SQL Cross Apply and a MERGE

  2. 2

    How to use a SQL Cross Apply and a MERGE

  3. 3

    Complex rolling scenario (CROSS APPLY and OUTER APPLY example)

  4. 4

    How can I use Cross Apply to multiple rows?

  5. 5

    How to apply lazy quantifier in this given scenario?

  6. 6

    How to apply conditional aggregation in SQL the following scenario?

  7. 7

    How to use collect() in drools in this scenario

  8. 8

    How to apply CROSS JOIN in PostgreSQL?

  9. 9

    Sharepoint: How to show AppendOnlyHistory on a display template in a cross-publishing scenario

  10. 10

    how to use insert to update columns from another table with user defined function cross apply

  11. 11

    How to use RethinkDB indices in the following scenario?

  12. 12

    How would I use VCR (with WebMock) in this scenario?

  13. 13

    How to use threading or Task pattern in following scenario

  14. 14

    How to use 2D vector in this scenario?

  15. 15

    How to use expandable list view in the following scenario

  16. 16

    How to use threading or Task pattern in following scenario

  17. 17

    How to use bluebird promise for specific scenario

  18. 18

    Confused in how to apply foreign key concept in my scenario

  19. 19

    When should I use cross apply over inner join?

  20. 20

    How to use scons for cross build

  21. 21

    How to use cross validation in MATLAB

  22. 22

    How to use variable extracted in JSON path extractor for further steps of scenario?

  23. 23

    Xcode 5, how to use autolayout with orientation, in the shown scenario?

  24. 24

    How to use create event on a jQuery dialog in following scenario?

  25. 25

    How do I use unique_ptr in this scenario?

  26. 26

    How can I use lambdaj groups for default case scenario

  27. 27

    How to use saved variable values outside of gatling scenario in scala file

  28. 28

    How to access json data and use in if condition in following scenario?

  29. 29

    How to use variable extracted in JSON path extractor for further steps of scenario?

HotTag

Archive