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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT Problem

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 555555
2 777777
3 777777

I 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_NUM
HAVING (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 Request
GROUP BY Ticket_Num
HAVING COUNT(*) > 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

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 555555
2 777777
3 777777

I 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_NUM
HAVING (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_Num
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY Ticket_Num ORDER BY ID) AS RowNo,
*
FROM Request
)t
WHERE t.RowNo >1
Go to Top of Page

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.
Go to Top of Page

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_Num
FROM Request
GROUP BY Ticket_Num
HAVING COUNT(*) >1
)t
Go to Top of Page

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 @table1
GROUP BY Ticket_Num
HAVING COUNT(*) > 1

SELECT COUNT(*)
FROM
(
SELECT COUNT(*) AS DupCount
FROM @table1
GROUP BY Ticket_Num
HAVING COUNT(*) > 1
) t

[/code]

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -