Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return rows with count > 1

Posted on 2008-10-03
15
Medium Priority
?
194 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
[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
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 32

Expert Comment

by:Brendt Hess
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:
Brendt Hess earned 2000 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:Brendt Hess
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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