Solved

Show records where value of one field if different than others, based on a group of fields

Posted on 2011-02-28
4
272 Views
Last Modified: 2012-05-11
Ok, to the title of this question is horrible.. I dont know how to phrase it any better :)

I have a table, called SalesUnitOfMeasuers
This table has 4 fields: RecID, Salesperson, Item and UnitOfMeasure (UoM)
What I want to know is anytime a salesperson sales an item with different UoM than their other sales of the same item.. by salesperson and item.  

For example, Jim sold pencils as each and then case.  I want to know this.
Steve sold pen as Each and then Box, I want to know this.
Heather is fine, she didnt mess up... She sold pencils twice, but both times as Each.

I dont care if the results show all of Jim's records and they show all of Steves records, or just the two different values (Jim+each and then Jim+Case).

This table is an example, so dont write code specifically saying "where UoM= 'EACH'" or anything like that. The UoM could be ROLL and then Case12.  I need a generic type of T-SQL that I can adapt for these types of situations, the table I am providing is just a sample.  

Basically, I dont know how to do this using T-SQL... I could use a cursor, but I dont want to do it that way unless I absolutely need to.

RecID      Salesperson      Item      UoM
1      Jim      Pencil      Each
2      Jim      Pencil      Each
3      Jim      Pencil      Case
4      Heather      Pencil      Each
5      Heather      Pencil      Each
6      Heather      Pen      Each
7      Steve      Pencil      Each
8      Steve      Pen      Each
9      Steve      Pen      Box

Thanks!
0
Comment
Question by:tfsaccount
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
check out this:
select SalesPerson, Item, min(UoM), max(UoM)
  from yourtable
 group by SalesPerson, Item
  having min(UoM) <> max(UoM) 

Open in new window

0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
Comment Utility
select distinct
	s.salesperson, s.item, s.uom
from
	salesunitofmeasures s
	inner join (
		select salesperson, item, count(distinct uom) c
		from salesunitofmeasures
		group by salesperson, item
	) s1
		on	s.salesperson = s1.salesperson 
			and s.item = s1.item
			and s1.c > 1

Open in new window

0
 
LVL 1

Author Closing Comment

by:tfsaccount
Comment Utility
This one worked perfectly, thanks!
0
 
LVL 1

Author Comment

by:tfsaccount
Comment Utility
The first one didnt give the same results as the second one, thanks though :)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now