Solved

return rows with count > 1

Posted on 2008-10-03
15
190 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Results to Excel File 18 89
SQL Query Syntax Assistance 2 44
STUFF function - proper use to update column? 6 16
SqlServer amend PK column 5 11
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 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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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