Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify The number of occurrences of a given field 1

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hello... I have the following data:

Code:
create table #temp (instrumentID varchar(25), saddition varchar(50), block varchar(10), Lot varchar(10), LotLength int)
insert into #temp values ('aa11', 'SP1', '5', '9', 1)
insert into #temp values ('aa11', 'SP1', '5', '10', 2)
insert into #temp values ('aa12', 'SP5', '9', '10', 2)
insert into #temp values ('aa12', 'SP5', '9', '11', 2)
insert into #temp values ('aa12', 'SP5', '9', '12', 2)
insert into #temp values ('aa12', 'SP6', '10', '5', 1)
insert into #temp values ('aa12', 'SP6', '10', '10', 2)
insert into #temp values ('aa13', 'SP7', '123', '31', 2)
insert into #temp values ('aa13', 'SP7', '123', '32', 2)

I need to know each instrumentid, saddition, block where multiple lotlengths exist exit per each instrumentid, saddition, block.

In this example:
'aa11' has 2 separate lotlengths for 'aa11', 'sp1', '5'
'aa12' has 2 separate lotlengths for 'aa12', 'sp6', '10'

I'm hoping for a query that can show this. I just can't get my grouping right.

I tried:
Code:
select distinct instrumentid, saddition, block
from #temp
group by instrumentid, saddition, block
having count(block) > 1

But this doesn't work.
Thanks!
 
I have found that the syntax for DISTINCT operates differently across various databases. Here is a trick that works on all databases - concatenating the fields.

select distinct(instrumentid + saddition + block)
from #temp
group by (instrumentid + saddition + block)
having count(instrumentid + saddition + block) > 1

If you need the individual columns, then add them to the query, make that query a subquery, and select the individual fields from the subquery.

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


 
If it "worked like a charm", how about awarding John a Star...[ponder]
Click on [blue]Great Post![/blue] link on his reply.


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top