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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return consecutive no show appointments 1

Status
Not open for further replies.

tipofapin

Technical User
May 6, 2010
6
US
Using SQL 2005 and SSRS 2008 R2. I have a list of clients who have had appointments within a date range. So I have columns with Client name, appointment date and time, and Appt Kept (Y/N). I need to return those who have consecutive no show appointments. That is for 2 or more appointments in a row, their 'Appt Kept' is 'N'. Is there a way to do this?
 
I would be doing this in the SQL query

Effectively do 2 queries of the data with a rownumber partioned by client (i.e. restarts on change of client

assuming a simple table called Appointments with clientName, appt_date, Attended as the columns you can use:
Code:
SELECT * FROM Appointments
WHERE Client_Name IN
(
Select Distinct Client_Name
FROM
(SELECT c1.ClientName, c1.appt_date as dt1, c1.attended as Attended1, c2.Appt_Date as dt2, c2.Attended as Attended2
FROM
(SELECT Client_Name, Appt_date, Attended, row_number() over (partition by Client_Name order by Appt_Date as incr
 FROM Appointments
) c1
inner join
(SELECT Client_Name, Appt_date, Attended, row_number() over (partition by Client_Name order by Appt_Date as incr
 FROM Appointments
) c2
on c1.Client_Name = c2.Client_Name and c1.incr = c2.incr -1
) ConsecutiveMisses
where Attendd1 = 'N' and Attended2 = 'N'
)

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top