Solved

SQL Server - Count Rows and Where Row Count = 1

Posted on 2015-02-10
7
163 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 51

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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 69

Accepted Solution

by:
Scott Pletcher earned 500 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

635 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