Solved

return rows with count > 1

Posted on 2008-10-03
15
187 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
Comment Utility
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
Comment Utility
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
Comment Utility
Oops.... 101 is the correct format code, not 109.
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks for the info ... but I already tried it and works.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
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
Comment Utility
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
Comment Utility
Check his output that he needs :)
0
 
LVL 11

Expert Comment

by:indianguru2
Comment Utility
Sorry .. my mistake ... :)
0
 

Author Comment

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

Expert Comment

by:BrandonGalderisi
Comment Utility
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
Comment Utility
Please check my code - it returns the case_id etc.
0
 

Author Comment

by:johnnyg123
Comment Utility
bhess1,

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

9 Experts available now in Live!

Get 1:1 Help Now