Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how many extra RAM for SQL server is needed 22 40
Error when saving to sql table a '/' 5 28
Replace Dates in query 14 40
Query group by data in SQL Server - cursor? 3 34
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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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