Code: Select all
SET @DateDone='2014-02-04';
DROP TABLE IF EXISTS t1,t2;
CREATE TABLE t1(
Seen int NOT NULL DEFAULT 0,
Sched int NOT NULL DEFAULT 0,
Rate CHAR(4) NOT NULL DEFAULT 0);
INSERT INTO t1(Seen,Sched,Rate)
SELECT COUNT(DISTINCT PatNum) AS 'Seen', '' AS 'Sched', '' AS 'Rate'
FROM appointment
WHERE DATE(AptDateTime)=@DateDone AND AptStatus=2;
CREATE TABLE t2
SELECT COUNT(DISTINCT a1.PatNum) AS 'Sched'
FROM appointment a1 LEFT JOIN appointment a2 ON a1.PatNum=a2.PatNum
WHERE (DATE(a1.AptDateTime)=@DateDone AND a1.AptStatus=2) AND (DATE(a2.AptDateTime)>@DateDone AND a2.AptStatus=1);
UPDATE t1,t2 SET t1.Sched=t2.Sched;
DROP TABLE IF EXISTS t2;
UPDATE t1 SET RATE=CONCAT(ROUND((Sched/Seen*100),0),'%');
SELECT * FROM t1
Code: Select all
SET @FromDate='2014-01-01' , @ToDate='2014-01-31';
DROP TABLE IF EXISTS t1,t2;
CREATE TABLE t1(
Date date,
Seen int NOT NULL DEFAULT 0,
Sched int NOT NULL DEFAULT 0,
Rate CHAR(4) NOT NULL DEFAULT 0);
INSERT INTO t1(Date,Seen)
SELECT DATE(AptDateTime) AS 'Date', COUNT(DISTINCT PatNum) AS 'Seen'
FROM appointment WHERE (DATE(AptDateTime) BETWEEN @FromDate AND @ToDate) AND AptStatus=2
GROUP BY Date;
CREATE TABLE t2
SELECT DATE(a1.AptDateTime) AS 'Date', COUNT(DISTINCT a2.PatNum) AS 'Sched'
FROM appointment a1 LEFT JOIN appointment a2 ON a1.PatNum=a2.PatNum
WHERE (DATE(a1.AptDateTime) BETWEEN @FromDate AND @ToDate AND a1.AptStatus=2) AND (DATE(a2.AptDateTime)>DATE(a1.AptDateTime) AND a2.AptStatus=1)
GROUP BY Date;
UPDATE t1,t2 SET t1.Sched=t2.Sched;
DROP TABLE IF EXISTS t2;
UPDATE t1 SET RATE=CONCAT(ROUND((Sched/Seen*100),0),'%');
SELECT * FROM t1;
Code: Select all
SELECT DATE(a1.AptDateTime) AS 'Date', COUNT(DISTINCT a2.PatNum) AS 'Sched'
FROM appointment a1 LEFT JOIN appointment a2 ON a1.PatNum=a2.PatNum
WHERE (DATE(a1.AptDateTime) BETWEEN @FromDate AND @ToDate AND a1.AptStatus=2) AND (DATE(a2.AptDateTime)>DATE(a1.AptDateTime) AND a2.AptStatus=1)
GROUP BY Date;
Code: Select all
WHERE (DATE(a1.AptDateTime) BETWEEN @FromDate AND @ToDate AND a1.AptStatus=2) AND (DATE(a2.AptDateTime)>DATE(a1.AptDateTime) AND a2.AptStatus=1)
My head hurts trying to explain that above example. I could also try to make sure the date that the appointment was created falls outside that date range, but that seems too complex at this point.
Any suggestions?