Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Serious Sam
Starting Member
4 Posts |
Posted - 2008-05-23 : 14:37:28
|
| I'm having trouble getting a SELECT statement to give me the results I need. Here's a sample of my table:Table: Request ID | Ticket_Num | fName | lName ...1 5555552 7777773 777777I need to be able to return the NUMBER (COUNT) of rows that have duplicate ticket numbers (NOT the actual ticket_num values). I just need the query to return 1 result for each row that has other duplicate rows, and not the total numebr of duplicates. So in the case above it would return a value of 1 because there is one row that has duplicates. I hope that makes sense. Here's my query right now:SELECT COUNT(Ticket_Num)FROM Request GROUP BY Ticket_NUMHAVING (COUNT(Ticket_Num) > 1)I thought this would do it, but it returns the actual ticket value that has duplicates (777777) and not the count. Thanks in advance. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-23 : 14:41:20
|
| SELECT COUNT(*)FROM RequestGROUP BY Ticket_NumHAVING COUNT(*) > 1Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-23 : 14:53:00
|
quote: Originally posted by Serious Sam I'm having trouble getting a SELECT statement to give me the results I need. Here's a sample of my table:Table: Request ID | Ticket_Num | fName | lName ...1 5555552 7777773 777777I need to be able to return the NUMBER (COUNT) of rows that have duplicate ticket numbers (NOT the actual ticket_num values). I just need the query to return 1 result for each row that has other duplicate rows, and not the total numebr of duplicates. So in the case above it would return a value of 1 because there is one row that has duplicates. I hope that makes sense. Here's my query right now:SELECT COUNT(Ticket_Num)FROM Request GROUP BY Ticket_NUMHAVING (COUNT(Ticket_Num) > 1)I thought this would do it, but it returns the actual ticket value that has duplicates (777777) and not the count. Thanks in advance.
SELECT t.ID,t.Ticket_NumFROM(SELECT ROW_NUMBER() OVER(PARTITION BY Ticket_Num ORDER BY ID) AS RowNo,*FROM Request)tWHERE t.RowNo >1 |
 |
|
|
Serious Sam
Starting Member
4 Posts |
Posted - 2008-05-23 : 15:05:53
|
| Thanks for the replies, but neither of those worked...Visakh - I've tried yours and it returns a value of 2 with table shown in the example. It is returning all the duplicate entries. I only want a count of 1 per set of duplicate rows. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-23 : 15:08:18
|
quote: Originally posted by Serious Sam Thanks for the replies, but neither of those worked...Visakh - I've tried yours and it returns a value of 2 with table shown in the example. It is returning all the duplicate entries. I only want a count of 1 per set of duplicate rows.
Ok. I got you. Try this:-SELECT COUNT(*)FROM(SELECT Ticket_NumFROM RequestGROUP BY Ticket_NumHAVING COUNT(*) >1)t |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-23 : 15:10:22
|
| [code]DECLARE @table1 table (ID tinyint, Ticket_Num int)INSERT INTO @table1 VALUES(1, 555555)INSERT INTO @table1 VALUES(2, 777777)INSERT INTO @table1 VALUES(3, 777777)INSERT INTO @table1 VALUES(4, 999)INSERT INTO @table1 VALUES(5, 999)SELECT COUNT(*)FROM @table1GROUP BY Ticket_NumHAVING COUNT(*) > 1SELECT COUNT(*)FROM( SELECT COUNT(*) AS DupCount FROM @table1 GROUP BY Ticket_Num HAVING COUNT(*) > 1) t[/code]Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
Serious Sam
Starting Member
4 Posts |
Posted - 2008-05-23 : 15:37:46
|
quote: SELECT COUNT(*)FROM( SELECT COUNT(*) AS DupCount FROM @table1 GROUP BY Ticket_Num HAVING COUNT(*) > 1)
Perfect...worked like a charm...thank you both. |
 |
|
|
|
|
|
|
|