Solved

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

Posted on 2011-02-28
4
277 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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 108
display data in text field from data base for updating 6 78
Restrict result set 1 48
SQL Server Insert where not exists 24 57
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 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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

739 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