Solved

SQL Script to find differences

Posted on 2015-01-21
10
71 Views
Last Modified: 2015-01-22
I have a mystery to solve and need help creating the script to solve it. This involves lines on a sales order and the field called SHIPCODE.

Sales Order Line Table - SOPLINES
ORDERNMBR,LNITMSEQ,SHIPCODE

For any one order, the SHIPCODE should always be the same on all lines for the order. What I need to find are the orders where that is not the case. So what I need to find is something like this:

1000,16384,WAREHUSE
1000,32768,WAREHUSE
1010,16384,3RDSTREET
1010,32768,WAREHUSE
1010,49152,WAREHUSE

I need it to find order 1010 because at least one of the SHIPCODE is different. How would you structure the query to first identity all the SHIPCODE codes and then if they are all not the same to flag this order?
0
Comment
Question by:rwheeler23
  • 4
  • 3
  • 3
10 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 300 total points
ID: 40563537
Not quite sure what you want as output, but to identify those orders with more than one SHIPCODE

select ORDERNMBR
from SOPLINES
group by ORDERNMBR
having COUNT(DISTINCT SHIPCODE) > 1

Then if you need the distinct list of SHIPCODEs for those orders, then:

select distinct ORDERNMBR, SHIPCODE
from SOPLINES
INNER JOIN (
          select ORDERNMBR
          from SOPLINES
          group by ORDERNMBR
          having COUNT(DISTINCT SHIPCODE) > 1
) as derived ON SOPLINES.ORDERNMBR = derived.ORDERNMBR
0
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 200 total points
ID: 40563538
This should get you started.

select * from SOPLINES s1 JOIN SOPLINES s2 on (s1.ordernmbr=s2.ordernmbr) where s1.shipcode <> s2.shipcode

Here is a fiddle : http://sqlfiddle.com/#!3/07373/4

EDIT: This is a bit cleaner
SELECT s1.* FROM
   SOPLINES s1 JOIN SOPLINES s2
   ON (s1.ordernmbr=s2.ordernmbr)
WHERE s1.shipcode <> s2.shipcode
GROUP BY s1.ORDERNMBR, s1.LNITMSEQ, s1.SHIPCODE
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40563540
@portlet - you beat me by 18s.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:rwheeler23
ID: 40563554
You folks are outstanding. In case you are curious PorletPaul' s script runs in 1 second and Mankowitz's script takes 6 seconds.

My problem know is I found way too many of these records. Some people are not following the rules so now I have to be a bad guy and ferret out the offender(s).
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40563558
@portlet - apparently, you beat me by 18+5 seconds.
0
 

Author Comment

by:rwheeler23
ID: 40563565
Well both of you are great in my book. I really appreciate the help. Now comes the tough part; assigning points.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40563567
@mankowitz, LOL it's not that common for me to be first in

-----------

@rwheeler23

Part of the problem here is the design. IF SHIPCODE is a 1:1 relationship with ORDERNMBR, then SHIPCODE should be an attribute of the order, not of the order lines.
0
 

Author Comment

by:rwheeler23
ID: 40563578
If everyone was processing orders correctly then there would be a 1:1 relationship between SHIPCODE and ORDRNMBR. I got chewed today because the users are blaming my code for the instances where there are multiple SHIPCODE on the lines of the order. The only way this can happen is if users are making the change after the order is entered. They insist they are not doing that but they also insist that need that ability.  Notice the contradiction in what I just said.  I am just going to go in tomorrow and tell management to shove my findings up their ying-yang. They can't have it both ways.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40563671
"ying-yang" and "both ways" seems an appropriate response.
0
 

Author Closing Comment

by:rwheeler23
ID: 40564084
It was a pleasure. Thanks again.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question