Solved

Need Counts

Posted on 2016-10-31
11
61 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 49

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 49

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 29

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
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.

 

Author Comment

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

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 29

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

623 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