Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-28
4
Medium Priority
?
282 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 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Copy Database Wizard 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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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