Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 379059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:51:18+00:00 2026-05-12T14:51:18+00:00

This is an under-performing query that I’ve been trying to simplify but am at

  • 0

This is an under-performing query that I’ve been trying to simplify but am at a loss. Basically we are looking at Dynamics-SL (Solomon) tables to determine if something has shipped yet or not. Often people enter quantity as having shipped yet the event handling is incorrect as it hasn’t actually left the warehouse. So we look at the SOEvent table for items with the ‘NPAK’ or ‘PINV’ type and then compare against the quantity shipped in the SOShipLine table… Problem is we do this again in the WHERE clause so we end up with this super long query that I gotta think can be simplified, here it is (TIA):

SELECT     

SOHeader.OrdNbr, 
SOHeader.CustID, 
SOHeader.User9 AS ShipDate,
SOLine.LineRef, 
SOLine.InvtID, 
SOLine.QtyOrd, 

CASE WHEN SOShipHeader.InvcNbr = '' THEN

CASE WHEN (
            SELECT MIN(EventTime)  
            FROM  Solomon_SOEvent SOEvent WITH (NOLOCK)   
            WHERE  (SOEvent.EventType = 'NPAK' OR SOEvent.EventType = 'PINV') 
            AND SOEvent.OrdNbr = SOHeader.OrdNbr) IS NULL 
THEN 0 

ELSE (
        SELECT MAX(SOShipLine.QtyShip) 
        FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
        WHERE SOShipLine.InvtID = SOLine.InvtID 
        AND SOShipLine.SiteID = SOLine.SiteID 
        AND SOShipHeader.ShipperID = SOShipLine.ShipperID
        )
END 

ELSE
  ISNULL(
        (SELECT MAX(SOShipLine.QtyShip) 
         FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
         WHERE SOShipLine.InvtID = SOLine.InvtID 
         AND SOShipLine.SiteID = SOLine.SiteID
         AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
,0)

END

AS QtyShip_Corrected,

ISNULL(
    (SELECT MAX(SOShipLine.QtyShip) 
     FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
     WHERE SOShipLine.InvtID = SOLine.InvtID 
     AND SOShipLine.SiteID = SOLine.SiteID 
     AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
   ,0)

AS SOShipLineQtyShip,

QtyShip

FROM Solomon_SOHeader SOHeader  WITH (NOLOCK) 
INNER JOIN Solomon_SOLine SOLine  WITH (NOLOCK) ON SOHeader.OrdNbr = SOLine.OrdNbr 
INNER JOIN Solomon_SOShipHeader SOShipHeader  WITH (NOLOCK) ON SOShipHeader.OrdNbr = SOHeader.OrdNbr 
AND SOShipHeader.Cancelled = 0

WHERE SOHeader.Status = 'O'
AND SOLine.QtyShip > 0
AND CASE WHEN SOShipHeader.InvcNbr = '' 

THEN

CASE WHEN (
        SELECT     MIN(EventTime)  
        FROM  Solomon_SOEvent SOEvent WITH (NOLOCK)   
        WHERE (SOEvent.EventType = 'NPAK' OR SOEvent.EventType = 'PINV') 
        AND SOEvent.OrdNbr = SOHeader.OrdNbr) IS NULL 
THEN 0 

    ELSE (
            SELECT MAX(SOShipLine.QtyShip) 
            FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
            WHERE SOShipLine.InvtID = SOLine.InvtID 
            AND SOShipLine.SiteID = SOLine.SiteID 
            AND SOShipHeader.ShipperID = SOShipLine.ShipperID
         )
    END 
ELSE

    ISNULL(
            (SELECT MAX(SOShipLine.QtyShip) 
             FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
             WHERE SOShipLine.InvtID = SOLine.InvtID 
             AND SOShipLine.SiteID = SOLine.SiteID 
             AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
       ,0)

END 

  <> ISNULL(
            (SELECT MAX(SOShipLine.QtyShip) 
             FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
             WHERE SOShipLine.InvtID = SOLine.InvtID 
             AND SOShipLine.SiteID = SOLine.SiteID 
             AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
    ,0)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:51:18+00:00Added an answer on May 12, 2026 at 2:51 pm

    Strip off the where clause and do the top part as a sub-query.

    Then you can use a where clause against the column names. Something like this:

    select * 
    from  (
        SELECT     
            SOHeader.OrdNbr, 
            SOHeader.CustID, 
            SOHeader.User9 AS ShipDate,
            SOLine.LineRef, 
            SOLine.InvtID, 
            SOLine.QtyOrd, 
    
            CASE WHEN SOShipHeader.InvcNbr = '' THEN
                CASE WHEN (
                                SELECT MIN(EventTime)  
                                FROM  Solomon_SOEvent SOEvent WITH (NOLOCK)   
                            WHERE  (SOEvent.EventType = 'NPAK' OR SOEvent.EventType = 'PINV') 
                            AND SOEvent.OrdNbr = SOHeader.OrdNbr) IS NULL 
                    THEN 0 
    
                    ELSE (
                            SELECT MAX(SOShipLine.QtyShip) 
                            FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
                        WHERE SOShipLine.InvtID = SOLine.InvtID 
                        AND SOShipLine.SiteID = SOLine.SiteID 
                        AND SOShipHeader.ShipperID = SOShipLine.ShipperID
                        )
                END 
    
                ELSE
                  ISNULL(
                        (SELECT MAX(SOShipLine.QtyShip) 
                         FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
                         WHERE SOShipLine.InvtID = SOLine.InvtID 
                         AND SOShipLine.SiteID = SOLine.SiteID
                         AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
                ,0)
            END
            AS QtyShip_Corrected,
    
            ISNULL(
                (SELECT MAX(SOShipLine.QtyShip) 
                 FROM Solomon_SOShipLine SOShipLine WITH (NOLOCK) 
                 WHERE SOShipLine.InvtID = SOLine.InvtID 
                 AND SOShipLine.SiteID = SOLine.SiteID 
                 AND SOShipHeader.ShipperID = SOShipLine.ShipperID)
               ,0)
    
            AS SOShipLineQtyShip,1
    
            QtyShip
    
        FROM Solomon_SOHeader SOHeader  WITH (NOLOCK) 
        INNER JOIN Solomon_SOLine SOLine  WITH (NOLOCK) ON SOHeader.OrdNbr = SOLine.OrdNbr 
        INNER JOIN Solomon_SOShipHeader SOShipHeader  WITH (NOLOCK) ON SOShipHeader.OrdNbr = SOHeader.OrdNbr 
        AND SOShipHeader.Cancelled = 0
    
        WHERE SOHeader.Status = 'O'
        AND SOLine.QtyShip > 0
    ) a
    where QtyShip_Corrected <> SOShipLineQtyShip
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under the impression that this was formed correctly, but here it is
I can't seem to find any help on this but under the Net tab
This may fall under you can't, and there's no reason to anyway, but I'm
I have a program that is performing waaaay under par, and I would like
I've been looking over the following VB code: http://www.codeproject.com/KB/vb/toggleNetworkConn.aspx If you look under The
I am trying to find a quick elisp fix that makes w3m under Emacs
I've been searching a lot but didnt find a concrete solution for this. My
if I run this under c# from p in Addresses where p.Address2 == null
This works under the .net framework 3.5 client. This fails under the .net framework
I am executing this statement under while (($data=fgetcsv($this->fin,5000,;))!==FALSE) Now what I want in else

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.