Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Script to find differences

Posted on 2015-01-21
10
Medium Priority
?
75 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 49

Accepted Solution

by:
PortletPaul earned 1200 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 800 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 49

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 49

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

772 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