Link to home
Start Free TrialLog in
Avatar of dkilby
dkilbyFlag for Canada

asked on

Help With MS SQL Query

I need a query in MS SQL that shows me the count of records that meet the below criteria.

4 fields - CustomerName, Direction, Disposition, TimeStamp

I need all records where Direction = Outbound, Disposition = Voicemail, and there is a record after the timestamp from the first critera with the same customer name, direction = inbound.

Is this possible and if so how ?
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Is this possible... probably

and if so how ? .... by using a SQL query e.g.

select CustomerName, Direction, Disposition, TimeStamp
from sometable
where Direction = Outbound
and Disposition = Voicemail

You should provide sample data and the expected result to make this bit clear:

>>"and there is a record after the timestamp from the first criteria with the same customer name, direction = inbound."

Is the "first criteria"  Direction = Outbound?
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try
select *
from tab1
wheres CustomerName+ltrim(cast(TimeStamp as varchar)) in(select CustomerName+ltrim(cast(min(TimeStamp) as varchar)) from tab1 a
where CustomerName+ltrim(cast(TimeStamp as varchar)) > (select CustomerName+ltrim(cast(max(TimeStamp) as varchar))
from tab1
where Direction = 'Outbound' and Disposition = 'Voicemail'
group by CustomerName)
and direction = 'Inbound'
group by CustomerName)
go

Open in new window