Prøv at bruge sessionsvariabler eller abstrahere dem til en lagret procedure, noget som -
Først:
SELECT
@calldate:=`calldate`,
@peakchan:=MAX(concurrent)+1
FROM (
...
) AS baseview
GROUP BY calldate
Andet:
SELECT @longestcall:=MAX(duration) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY
For det tredje:
SELECT @totalmins:=SUM(duration) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY
For det fjerde:
SELECT @totalconfs:=COUNT(*) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY;
Og endelig
INSERT INTO bridgedb.stats (date, peakchan, longestcall, totalmins, totalconfs)
VALUES (@calldate, @peakchan, @longestcall, @totalmins, @totalconfs)
BTW:Det er ikke en god idé at kalde en kolonne for date
.
REDIGER
Selvfølgelig skal du sørge for, at disse forespørgsler kører i samme session for at bruge sessionsvariabler. Dette er standard, når det køres med en enkelt DB-forbindelse i PHP
REDIGERING 2
For at håndtere perioder uden aktivitet kan du
- enten præfiks dette med
SELECT @calldate:=DATE(NOW() - INTERVAL 1 DAY), @peakchan:=0, @longestcall:=0, @totalmins:=0, @totalconfs:=0;
(optag med oprettede standardindstillinger) - eller stop efter den første forespørgsel, hvis @calldate er NULL (ingen registrering oprettet)
REDIGERING 3
Dette virker for mig:
DELIMITER //
CREATE PROCEDURE ReportYesterday()
BEGIN
DECLARE calldate DATE;
DECLARE peakchan, longestcall, totalmins, totalconfs INT;
SELECT
@calldate:=`calldate`,
@peakchan:=MAX(concurrent)+1
FROM (
SELECT
DATE(a.calldate) as calldate,
COUNT(b.uniqueid) AS concurrent
FROM asteriskcdr.cdr AS a, asteriskcdr.cdr AS b
WHERE
a.calldate >= NOW() - INTERVAL 1 DAY
AND (
(a.calldate<=b.calldate AND (UNIX_TIMESTAMP(a.calldate)+a.duration)>=UNIX_TIMESTAMP(b.calldate))
OR (b.calldate<=a.calldate AND (UNIX_TIMESTAMP(b.calldate)+b.duration)>=UNIX_TIMESTAMP(a.calldate))
)
AND a.uniqueid>b.uniqueid
GROUP BY a.uniqueid) AS baseview
GROUP BY calldate
;
-- EDIT 4 IS THE FOLLOWING LINE
IF @calldate IS NOT NULL THEN
SELECT @longestcall:=MAX(duration) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY
;
SELECT @totalmins:=SUM(duration) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY
;
SELECT @totalconfs:=COUNT(*) FROM bridgedb.log WHERE `start` >= NOW() - INTERVAL 1 DAY
;
INSERT INTO bridgedb.stats (date, peakchan, longestcall, totalmins, totalconfs)
VALUES (@calldate, @peakchan, @longestcall, @totalmins, @totalconfs)
;
END IF;
END
;
//
DELIMITER ;
efterfulgt af
CALL ReportYesterday();