Solved

Need Counts

Posted on 2016-10-31
11
50 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:charlesbaldo
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:charlesbaldo
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:charlesbaldo
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

785 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