Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateDiff and Group by queries

Status
Not open for further replies.

fredong

IS-IT--Management
Sep 19, 2001
332
US
Here is the scenario I have Customers that called in and create helpdesk ticketno and I need to create a query that show the same customer who created 2 or more tickets withthin the 30 days period and showed the last Ticketno and last createdate.There are 2 tables and 3 fields invloved the Customer.custid,Ticket.createdate,Ticket.ticketno.Any ideas ? Below is my example but the syntaxs are wrong. Please help.Thanks.

select count(*),a.custid, b.createdate from ticket a, customer b where
a.custkey =b.custkey
and DATEDIFF(Day,Max(a.CREATEDATE),GETDATE())= 30)
group by b.custid
Having Count(*) > 1
 
you are having an extra brace in the end
DATEDIFF(Day,Max(a.CREATEDATE),GETDATE())= 30[red])[/red]

remove it...

-DNG


 
Please post what errors you are getting when you run your query.

Thanks,


Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
I have removed the ")"and still I got an error below

Server: Msg 147, Level 15, State 1, Line 3
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
 
The createDate in your select list is not pulling from the same table as the createDate in your DateDiff statement. Try putting your DateDiff statement in the Having clause instead of the WHERE clause.







Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
OK I have the corrections of the createdate and custid so what would be the correct syntax?Please advise.

select count(*),b.custid, a.createdate from ticket a, customer b where
a.custkey =b.custkey
group by b.custid
Having Count(DATEDIFF(Day,Max(a.CREATEDATE),GETDATE())= 30)) > 1
 
It's telling you that you can't use the MAX() in the WHERE.

-SQLBill

Posting advice: FAQ481-4875
 
Even though I removed the the MAX() still return the same error. The reason I using MAX is because I am trying to get the last ticket created from the same customer.
 
fredong said:
Having Count(DATEDIFF(Day,Max(a.CREATEDATE),GETDATE())= 30)) > 1

FYI: This statement is always going to return false. You will never have a Count over 1 of the difference between two dates, because the difference is only returning a single value. You need to remove the Count()>1 part or Count something else and have the DateDiff as a second part of the Having statement.

If your code is still returning the same error something else is wrong. Break it down to the original select without the Group By and Having. What sorts of results are you getting? Order them by custID to see if there are indeed multiple "counts". Look at the dates to see if there is multiple counts within your 30 day window. Let us know what you see.



Catadmin - MCDBA, MCSA
"If a person is Microsoft Certified, does that mean that Microsoft pays the bills for the funny white jackets that tie in the back???
 
select a.custid, MAX(b.createdate)
from ticket a, customer b
where
a.custkey = b.custkey
and DATEDIFF(Day,a.CREATEDATE,GETDATE())= 30
group by b.custid
 
Hi jbenson001,
I need another step that I have mentioned above is to show only custid that have created 2 or more tickets withthin the 30 days period and showed the last Ticketno and last createdateAny ideas?Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top