Solved

return rows with count > 1

Posted on 2008-10-03
15
189 Views
Last Modified: 2010-03-20
I have the following sql server 2005 table


case_#     customer_code          call_date_time
1                1234                          09/09/2008 10:12:12
2                1234                          09/09/2008 13:12:12
3                1234                          09/09/2008 14:12:12
4                1234                          09/10/2008 14:14:12
5                2456                          09/12/2008 14:14:12
6                2457                          09/12/2008 15:20:12
7                2457                          09/12/2008 16:14:12


I am trying to write a query that will return all rows in which the customer has called more than once in a given day.

Using the above data, I am looking for the output to be as follows:

1                1234                          09/09/2008 10:12:12
2                1234                          09/09/2008 13:12:12
3                1234                          09/09/2008 14:12:12
6                2457                          09/12/2008 15:20:12
7                2457                          09/12/2008 16:14:12






0
Comment
Question by:johnnyg123
  • 4
  • 4
  • 3
  • +2
15 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22634749
select customercode, convert(datetime,convert(varchar(11),call_date_time,109),109)
from YourCustomers
group by customercode, convert(datetime,convert(varchar(11),call_date_time,109),109)
having count(*)>1
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22634782
select customercode, convert(varchar(10),call_date_time,101), count(*)
from tablename
group by customercode, convert(varchar(10),call_date_time,101)
having count(*)>1
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22634808
Oops.... 101 is the correct format code, not 109.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Expert Comment

by:bhess1
ID: 22634847
This will return the rows you are looking for:

SELECT
    case_#,
    customer_code,
    call_date_time
FROM MyTable t
INNER JOIN (
    SELECT
        customer_code,
        Cast(Convert(varchar(8), call_date_time, 112) as datetime) as Dt,
        Count(*) As Ct
    FROM MyTable
    GROUP BY
        customer_code,
        Cast(Convert(varchar(8), call_date_time, 112) as datetime)
    HAVING count(*) > 1
    ) Filter
    ON t.customer_code = filter.customer_code
    AND t.call_date_time >= filter.dt
    AND t.call_date_time < Dateadd(day, 1, filter.dt)
0
 
LVL 11

Expert Comment

by:indianguru2
ID: 22634915

SELECT  t.case_#,
        t.customer_code,
        t.call_date_time
FROM    Table_1 t
        INNER JOIN ( SELECT customer_code
                     FROM   dbo.Table_1
                     GROUP BY customer_code
                     HAVING COUNT(customer_code) > 1
                   ) i ON t.customer_code = i.customer_code

Open in new window

0
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 22634979
Heh - thanks for the reminder on the table alias in the SELECT statement, indianguru2.  Unfortunately, your query will not succeed, since it would return record #4 as well
SELECT
    t.case_#,
    t.customer_code,
    t.call_date_time
FROM MyTable t
INNER JOIN (
    SELECT
        customer_code,
        Cast(Convert(varchar(8), call_date_time, 112) as datetime) as Dt,
        Count(*) As Ct
    FROM MyTable
    GROUP BY
        customer_code,
        Cast(Convert(varchar(8), call_date_time, 112) as datetime)
    HAVING count(*) > 1
    ) Filter
    ON t.customer_code = filter.customer_code
    AND t.call_date_time >= filter.dt
    AND t.call_date_time < Dateadd(day, 1, filter.dt)

Open in new window

0
 
LVL 11

Expert Comment

by:indianguru2
ID: 22635030
Thanks for the info ... but I already tried it and works.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635120
indianguru2:

No it won't.  your grouping  has NO aggregation to ensure that the calls occurred on the same day.  Yours just checks for more than one call which was not the requirement.
0
 

Author Comment

by:johnnyg123
ID: 22635149
hmmmm....

I was hoping to get case_id in the result set instead of the count but when I try to add case_id I get the dreaded

case_id is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I need the actual case numbers not just how many times it occurs
0
 
LVL 11

Expert Comment

by:indianguru2
ID: 22635151
Check his output that he needs :)
0
 
LVL 11

Expert Comment

by:indianguru2
ID: 22635158
Sorry .. my mistake ... :)
0
 

Author Comment

by:johnnyg123
ID: 22635161
oops....looks like I posted my commment before I hit refresh
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22635218
What case # do you want?  All of them for a given day?

if so, use bhess1's solution but you may need to wrap t.case_# in brackets like this...


t.[case_#]
0
 
LVL 32

Expert Comment

by:bhess1
ID: 22635281
Please check my code - it returns the case_id etc.
0
 

Author Comment

by:johnnyg123
ID: 22635446
bhess1,

you are correct....your code is exactly what I was looking for!!!!!!!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.

830 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