Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Script to find differences

Posted on 2015-01-21
10
Medium Priority
?
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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