Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server - Count Rows and Where Row Count = 1

Posted on 2015-02-10
7
Medium Priority
?
168 Views
Last Modified: 2015-02-10
I have a table called Asset that has the following attributes:

Assetnum
Description
Location

I want to group this by location and show only locations that have exactly one assetnum. If a location has more than one asset, it should be exclued from the result set. I need the result set to show the location, the assetnum, and the description

Here is an example of the table

------------------------------------------------------------
Assetnum          Description          Location
------------------------------------------------------------
0101                   Asset 0101           A
0102                   Asset 0102           B
0103                   Asset 0103           C
0104                   Asset 0104           B
0105                   Asset 0105           B
0106                   Asset 0106           A
0107                   Asset 0107           D
-------------------------------------------------------

The result that I am looking for is:

----------------------------------------------------------------------------------------
Assetnum          Description          Location       Count of Assets
----------------------------------------------------------------------------------------
0103                   Asset 0103           C                               1
0107                   Asset 0107           D                               1
-----------------------------------------------------------------------------------------
0
Comment
Question by:cbridgman
[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
7 Comments
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40601098
SELECT Assetnum, Description, Location, COUNT(*)
FROM Asset
WHERE <YourSearchCriteria>
GROUP BY Assetnum, Description, Location
HAVING COUNT(*)=1

Open in new window

0
 

Author Comment

by:cbridgman
ID: 40601125
That query returns every row in the Asset table.  We have 2500 assets (rows) in our table. Each asset is associated with a location. If you look through the table and group by location, you will find that some locations appear in many rows while other locations appear in only 1 row. In my example, Locations A and B appear in multiple rows. Locations C and D appear in only one row. C & D are the the ones I want returned, not A & B.

Does that make sense?
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40601148
Adding to the above solution:

SELECT Assetnum, Description, Location, COUNT(*)
FROM Asset
WHERE <YourSearchCriteria>
GROUP BY Assetnum, Description, Location
HAVING COUNT(*)=1 AND Location IN(D, C)
0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

Author Comment

by:cbridgman
ID: 40601161
What does the IN(D,C) mean in the Having clause?  I get an error when i run it that tells me D and C are invalid column names
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40601164
SELECT
    MAX(Assetnum,) AS Assetnum, MAX(Description) AS Description,
    Location, COUNT(*) AS [Count of Assets]
FROM Asset
GROUP BY Location
HAVING COUNT(*) = 1
0
 

Author Closing Comment

by:cbridgman
ID: 40601171
Thanks Scott, that did the trick
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40601176
SELECT Assetnum, Description, Location, COUNT(*)
FROM Asset
WHERE Location IN(D, C)
GROUP BY Assetnum, Description, Location
HAVING COUNT(*)=1

In(D, C) is to ignore other locations.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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