Solved

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

Posted on 2011-02-28
4
278 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
[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
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34997362
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
ID: 34997395
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
ID: 34998557
This one worked perfectly, thanks!
0
 
LVL 1

Author Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

636 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