Solved

Need Counts

Posted on 2016-10-31
11
56 Views
Last Modified: 2016-11-17
I have two columns in a sql server table

LanID,   SerialNumber
100        101
101        101
102        987
103        622
104        733
104        733
105        626
106        626
107        626

Each LanID should have a unique serial number.   I need a list of SerialNumbers that has more than one LanID

Please I need a TSQL that gives me the SerialNumbers that have more than one LanID and a count of them
0
Comment
Question by:Charles Baldo
[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
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41867727
SELECT
      *
FROM (
      SELECT
            LanID
          , SerialNumber
          , COUNT(*) OVER (PARTITION BY LanID ORDER BY SerialNumber) LanID_count
          , COUNT(*) OVER (PARTITION BY SerialNumber ORDER BY LanID) SerialNumber_count
    FROM your-table-here
    ) d
WHERE LanID_count > 1
      OR SerialNumber_count > 1

Open in new window

{+edits} sorry please ensure you refresh
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41867733
goodness, what was I thinking. started with row_number() in mind, then halfway swapped to count() and butchered it. This is better:
SELECT
      *
FROM (
      SELECT
            LanID
          , SerialNumber
          , COUNT(*) OVER (PARTITION BY LanID) LanID_count
          , COUNT(*) OVER (PARTITION BY SerialNumber) SerialNumber_count
      FROM table1
    ) d
WHERE LanID_count > 1
      OR SerialNumber_count > 1
ORDER BY LanID

Open in new window

result looks like this:
| LanID | SerialNumber | LanID_count | SerialNumber_count |
|-------|--------------|-------------|--------------------|
|   100 |          101 |           1 |                  2 |
|   101 |          101 |           1 |                  2 |
|   104 |          733 |           2 |                  2 |
|   104 |          733 |           2 |                  2 |
|   105 |          626 |           1 |                  3 |
|   106 |          626 |           1 |                  3 |
|   107 |          626 |           1 |                  3 |

CREATE TABLE Table1
    ([LanID] int, [SerialNumber] int)
;
    
INSERT INTO Table1
    ([LanID], [SerialNumber])
VALUES
    (100, 101),
    (101, 101),
    (102, 987),
    (103, 622),
    (104, 733),
    (104, 733),
    (105, 626),
    (106, 626),
    (107, 626)
;

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41867746
Try.. Also note that Order by is not required in case of COUNT(*) OVER (PARTITION BY Col.)

--

CREATE TABLE Table1
    ([LanID] int, [SerialNumber] int)
GO
    
INSERT INTO Table1
    ([LanID], [SerialNumber])
VALUES
    (100, 101),
    (101, 101),
    (102, 987),
    (103, 622),
    (104, 733),
    (104, 733),
    (105, 626),
    (106, 626),
    (107, 626)
GO

--

Open in new window


Query
----

--


SELECT * FROM 
(
	SELECT * , COUNT(*) OVER (PARTITION BY [SerialNumber]) Cnt
	FROM Table1 
)k
WHERE Cnt > 1

--

Open in new window



O/p
-------------
LanID      SerialNumber      Cnt
100                      101                       2
101                      101                       2
105                      626                       3
106                      626                       3
107                      626                       3
104                      733                       2
104                      733                       2

Do you want Lan Ids information also along with the above info?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Charles Baldo
ID: 41867751
Thank You
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41867784
This is better:
and I corrected myself with the second comment, you will notice there is no order by in that

30 minutes later someone else points out the error I already admitted to, and corrected?

curious that such unfriendly behaviour should be rewarded
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41867795
Ohhh my bad !!, Didn't checked your second solution. Reopening the question and yes points should be awarded to you only.
@Author - Pls award to points to Paul. I didn't his second solution. :)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41868869
Wow, any Windowing function is overkill for this.  A standard GROUP BY and HAVING can handle this:

SELECT SerialNumber, COUNT(*) AS Total_Uses, MIN(LanID) AS LanID_Min, MAX(LanID) AS LanID_Max
FROM table_name
GROUP BY SerialNumber
HAVING COUNT(*) > 1

If there's more than two for a given serial number, and you need to lookup them all up, an outer query to handle that could easily be added as well, sample code below.  This does add some overhead, but probably not as much as another full scan for the Windowing(s), especially if the table is properly indexed.

SELECT t.*
FROM (
    SELECT SerialNumber, COUNT(*) AS Total_Uses, MIN(LanID) AS LanID_Min, MAX(LanID) AS LanID_Max
    FROM table1
    GROUP BY SerialNumber
    HAVING COUNT(*) > 1
) AS dups
INNER JOIN Table1 t ON t.SerialNumber = dups.SerialNumber AND t.LanID BETWEEN dups.LanID_Min AND dups.LanID_Max
ORDER BY t.SerialNumber, t.LanID
0
 

Author Comment

by:Charles Baldo
ID: 41872682
My apologies, was moving fast needed solution

The solution worked, was not my intention to misappropriate points.

Hope all is correct
Thank you to all who helped.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

756 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