回答
我真的很努力工作了什麼你的SQL試圖擺在首位。
它似乎是在最小和最大秒之間(好的,比最大日期時間多1秒)獲得每秒鐘的時間,並獲得記錄的數量。
如果是這樣,那麼最多〜1000秒應對: -
SELECT ADDDATE(MinCallDate, INTERVAL Sub1.i SECOND) AS aDate, COUNT(*)
FROM
(
SELECT MIN(calldate) AS MinCallDate, MAX(calldate) AS MaxCallDate
FROM calls
)Sub0
CROSS JOIN
(
SELECT units.i + tens.i * 10 + hundreds.i * 100 as i
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds
) Sub1
INNER JOIN
(
SELECT calldate, ADDDATE(calldate, INTERVAL billsec SECOND) AS callenddate
FROM calls
) b
ON DATE_FORMAT(ADDDATE('2013-05-14 09:40:30', INTERVAL Sub1.i SECOND),"%Y%m%d%H%i%s") BETWEEN b.calldate AND b.callenddate
WHERE ADDDATE(MinCallDate, INTERVAL Sub1.i SECOND) <= MaxCallDate
AND Sub1.i < TIMESTAMPDIFF(SECOND,'2013-05-14 09:40:30', '2013-05-14 09:41:00')
GROUP BY aDate
如果你能提供你正在努力實現我也許能拿出更好的東西什麼的非SQL解釋。
編輯 - 很簡單的計數: -
SELECT Sub1.TimeSecond, COUNT(Sub2.CallSecond)
FROM
(
SELECT ADDDATE('2013-05-14 09:40:30', INTERVAL units.i + tens.i * 10 + hundreds.i * 100 + thousands.i * 1000 SECOND) AS TimeSecond
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) thousands
HAVING TimeSecond BETWEEN '2013-05-14 09:40:30' AND '2013-05-14 09:41:00'
) Sub1
LEFT OUTER JOIN
(
SELECT ADDDATE(calldate, INTERVAL units.i + tens.i * 10 + hundreds.i * 100 + thousands.i * 1000 SECOND) AS CallSecond
FROM calls
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) thousands
WHERE units.i + tens.i * 10 + hundreds.i * 100 + thousands.i * 1000 <= billsec
) Sub2
ON Sub1.TimeSecond = Sub2.CallSecond
GROUP BY Sub1.TimeSecond
這將應付的範圍/手機通話9999秒(易於擴展它更大的範圍內,但會使其速度較慢但不能肯定。所有這些都將是有效,因爲MySQL不能真正使用索引的任何連接
簡單會是這樣,因爲它不是每個通話時長中產生,每次: - 。
SELECT Sub1.TimeSecond, COUNT(calls.calldate)
FROM
(
SELECT ADDDATE('2013-05-14 09:40:30', INTERVAL units.i + tens.i * 10 + hundreds.i * 100 + thousands.i * 1000 SECOND) AS TimeSecond
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) thousands
HAVING TimeSecond BETWEEN '2013-05-14 09:40:30' AND '2013-05-14 09:41:00'
) Sub1
LEFT OUTER JOIN calls
ON Sub1.TimeSecond BETWEEN calls.calldate AND ADDDATE(calls.calldate, INTERVAL calls.billsec SECOND)
GROUP BY Sub1.TimeSecond
你可以改變表格的佈局嗎?如果是這樣,爲呼叫結束日期時間添加一列可能會有所幫助。
我需要計算給定時間間隔的呼叫峯值數,實際上一行需要在兩次之間爲一秒,如果我設置了一個時間間隔,例如2013-05-14 09:00:00至2013-05- 14 10:00:00需要有3600條記錄。我還沒有想過如何優化一個查詢比現在更快:)。謝謝 –
所以表格調用每次調用一行,包括該調用的開始時間和該調用的長度。您需要2個日期/時間之間的所有時間(以秒爲單位)以及此時當前的電話數量的計數? – Kickstart
正確的是,此刻當前呼叫數量的計數 –
- 1. 相關的mysql子查詢
- 2. 相關子查詢的MySQL
- 3. MySQL相關子查詢FROM
- 4. MySQL相關子查詢
- 5. mySQL相關子查詢
- 6. MySQL的:相關子查詢選擇類型關聯子查詢
- 7. 將SQL查詢(與相關子查詢)轉換爲LINQ在C#
- 8. MYSQL查詢相關查詢
- 9. MySQL的相關子查詢SUM()ORDER BY
- 10. MySQL的 - 在相關子查詢
- 11. MySQL中的索引相關子查詢
- 12. MySql的相關子查詢執行
- 13. 如何用非相關子查詢替換相關的子查詢?
- 14. mysql相關查詢
- 15. 變換相關子查詢轉換爲連接
- 16. 如何將包含dublicate表的相關子查詢轉換爲不相關的子查詢?
- 17. 相關子查詢
- 18. 相關子查詢
- 19. 與查詢相關的MySQL查詢
- 20. MySQL相關子查詢:子查詢無法從外部查詢中查找表?
- 21. Mysql將子查詢轉換爲依賴子查詢
- 22. 將mysql子查詢轉換爲Laravel
- 23. Mysql子查詢轉換內部聯接
- 24. 轉換mysql子查詢加入
- 25. 與子查詢轉換MySQL來DQL 1.2
- 26. LINQ的相關子查詢
- 27. 將常規選擇語句轉換爲相關子查詢?
- 28. 轉換MySQL查詢SQLite的查詢
- 29. 轉換的Oracle查詢到MySQL查詢
- 30. 轉換查詢有mysql的
你想要返回什麼? – Kickstart