Solved

SQL Script to find differences

Posted on 2015-01-21
10
68 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now