Solved

SQL Server - Count Rows and Where Row Count = 1

Posted on 2015-02-10
7
153 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
7 Comments
 
LVL 46

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 33

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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:
ScottPletcher 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now