Sub Query - return only all orders ready to pick

  • I have a table with some data, Having hard time returning values based upon two conditions.

    Create Table X_ReadytoPick

    (

    SOPstatus varchar(50),

    OpsStatus varchar(50),

    OrderNumber varchar(50)

    )

    INSERT INTO X_ReadytoPick

    VALUES

    ('Acknowledgement','Okay','ORD001'),

    ('Pack Complete','Picked','ORD002'),

    ('Pack Complete','Picked','ORD002'),

    ('Pack Complete','Picked','ORD002'),

    ('DocsPrinted','Okay','ORD003'),

    ('Acknowledgement','NotYet','ORD004'),

    ('Acknowledgement','Okay','ORD004'),

    ('Acknowledgement','NotYet','ORD004')

    dataset1

    I want to return only two orders out of the dataset (8 records 4 orders) from above......

    ORD001 and Ord003 are the only orders that all rows for the order='Okay' (OpsStatus) and SOPstatus is in ('Acknowledgement','DocsPrinted')

    I know I have to create a subquery and grouping...……..

    select SOPstatus, OpsStatus, SopNumber from X_ReadytoPick

    where SOPstatus in ('Acknowledgement','DocsPrinted')

    gets me Orders ORD001,ORD002, ORD004 - But I don't want ORD004 since only 1 of the three records for ORD004 are 'Okay'

    Basically needing to identify all orders with all items 'okay' and ready to pick.....

    Thank you in advance

     

     

  • Like this?

    DROP TABLE IF EXISTS #X_ReadytoPick;

    CREATE TABLE #X_ReadytoPick
    (
    SOPstatus VARCHAR(50)
    ,OpsStatus VARCHAR(50)
    ,OrderNumber VARCHAR(50)
    );

    INSERT #X_ReadytoPick
    VALUES
    ('Acknowledgement', 'Okay', 'ORD001')
    ,('Pack Complete', 'Picked', 'ORD002')
    ,('Pack Complete', 'Picked', 'ORD002')
    ,('Pack Complete', 'Picked', 'ORD002')
    ,('DocsPrinted', 'Okay', 'ORD003')
    ,('Acknowledgement', 'NotYet', 'ORD004')
    ,('Acknowledgement', 'Okay', 'ORD004')
    ,('Acknowledgement', 'NotYet', 'ORD004');

    SELECT *
    FROM #X_ReadytoPick xrp
    WHERE NOT EXISTS
    (
    SELECT *
    FROM #X_ReadytoPick xrp2
    WHERE xrp.OrderNumber = xrp2.OrderNumber
    AND xrp2.OpsStatus <> 'Okay'
    )
    AND xrp.SOPstatus IN ('Acknowledgement', 'DocsPrinted');

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • SELECT OrderNumber
    FROM #X_ReadytoPick
    GROUP BY OrderNumber
    HAVING MAX(CASE WHEN OpsStatus = 'Okay' AND SOPstatus IN ('Acknowledgement', 'DocsPrinted') THEN 0 ELSE 1 END) = 0
    ORDER BY OrderNumber /*optional, of course*/

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Phil - your response worked beautifully for me. Thank you very much.

  • I will try this reply as well. Thank you so much for taking time to reply.

  • Scott - worked quite well as well. Nice to have two options to review and learn from. Your query requires grouping all the data elements but it's simplicity (even I understand it) it great.

  • Your Blog is very nice. Wish to see much more like this. Thanks for sharing your information

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply