Solved

SQL Script to find differences

Posted on 2015-01-21
10
70 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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 …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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