Solved

Need Counts

Posted on 2016-10-31
11
40 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 17

Expert Comment

by:Pawan Kumar Khowal
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
 

Author Comment

by:charlesbaldo
ID: 41867751
Thank You
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 17

Expert Comment

by:Pawan Kumar Khowal
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:ScottPletcher
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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

19 Experts available now in Live!

Get 1:1 Help Now