Solved

Need Counts

Posted on 2016-10-31
11
59 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
connection to SQL 2012 error in windows 10 18 47
SQL Query 9 28
database audit for object access 6 42
*** Windows Server 2012 Websites Set Up *** 17 29
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

734 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