Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SELECT only records where amount > 1

Author  Topic 

duanecwilson
Constraint Violating Yak Guru

273 Posts

Posted - 2010-03-29 : 18:43:04
I have a simple issue, but have a mental block. If I have a table with 2 columns, WorkstationID, and Product, and I want to select ONLY those workstations with more than one product installed on them (these are Office versions). Some workstations have 2 or more versions. If the Workstation only has 1, I don't want them. So the final result will have 2 columns - WorkstationID, Product. So it will look like this:
WorkstationID, Product
1656, Office XP
1656, Office 2003
1656, Office 2007
1795, Office 2003
1795, Office 2007
1831, Office XP
1831, Office 2003
2096, Office 2003
2096, Office 2007
etc. - no entries where only 1 version installed.
I have tried various sub-queries, group by, having, etc.
Thanks in advance for helping.


Duane

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2010-03-30 : 09:45:32
select a.workstationid,a.product
from TABLE a
inner join
(select workstationid
from TABLE
group by workstationid
having count(*) > 1) b
on a.workstationid = b.workstationid

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -