0
我正在使用SQLYog上的預定報告功能來發送我們生產線上的小時生產報告。我遇到的問題是如何讓我的查詢自動填充我的開始和結束日期。下面是我的查詢的一個例子:SQL查詢自動輸入日期變量
我基本上只需要自動填充第一個變量中的日期戳,因爲移位開始和停止時間總是相同的。
SET
@DayStart = '2013-11-06 07:00:00',
@DayEnd = '2013-11-06 16:00:00';
SELECT Product1.rec_cnt AS 'Handset',
CONCAT(Product1EOL1.rec_cnt, '/', Product1EOL2.rec_cnt) AS 'Handset EOL',
Product2.rec_cnt AS 'Wallet',
Product3.rec_cnt AS 'Wholesale',
CONCAT(Product3EOL1.rec_cnt, '/', Product3EOL2.rec_cnt) AS 'Wholesale EOL',
Product4.rec_cnt AS 'Blister',
CONCAT(Product4EOL1.rec_cnt, '/', Product4EOL2.rec_cnt) AS 'Blister EOL'
FROM
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '3'
GROUP BY StartHour, EndHour
) AS Product1
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '10'
AND status_id = '4'
GROUP BY StartHour, EndHour
) AS Product1EOL1
ON Product1.StartHour = Product1EOL1.StartHour AND Product1.EndHour = Product1EOL1.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '10'
AND status_id = '6'
GROUP BY StartHour, EndHour
) AS Product1EOL2
ON Product1.StartHour = Product1EOL2.StartHour AND Product1.EndHour = Product1EOL2.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '2'
GROUP BY StartHour, EndHour
) AS Product2
ON Product1.StartHour = Product2.StartHour AND Product1.EndHour = Product2.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '6'
GROUP BY StartHour, EndHour
) AS Product3
ON Product1.StartHour = Product3.StartHour AND Product1.EndHour = Product3.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '9'
AND status_id = '4'
GROUP BY StartHour, EndHour
) AS Product3EOL1
ON Product1.StartHour = Product3EOL1.StartHour AND Product1.EndHour = Product3EOL1.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '9'
AND status_id = '6'
GROUP BY StartHour, EndHour
) AS Product3EOL2
ON Product1.StartHour = Product3EOL2.StartHour AND Product1.EndHour = Product3EOL2.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '4'
GROUP BY StartHour, EndHour
) AS Product4
ON Product4.StartHour = Product3.StartHour AND Product4.EndHour = Product3.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '8'
AND status_id = '4'
GROUP BY StartHour, EndHour
) AS Product4EOL1
ON Product1.StartHour = Product4EOL1.StartHour AND Product1.EndHour = Product4EOL1.EndHour
INNER JOIN
(
SELECT StartHour, EndHour, COUNT(t_container.container_id) AS rec_cnt
FROM
(
SELECT DATE_ADD(@DayStart, INTERVAL i HOUR) AS StartHour, DATE_ADD(DATE_ADD(@DayStart, INTERVAL i HOUR), INTERVAL 3599 SECOND) AS EndHour
FROM
(
SELECT 0 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
) Sub1
) Sub2
LEFT OUTER JOIN t_container
ON created_timestamp BETWEEN StartHour AND EndHour
AND container_type_id = '8'
AND status_id = '6'
GROUP BY StartHour, EndHour
) AS Product4EOL2
ON Product1.StartHour = Product4EOL2.StartHour AND Product1.EndHour = Product4EOL2.EndHour
你的自動填充,你把它看起來是硬編碼的。爲什麼你能不能做一個GetDate()調用,以便在運行計劃時根據需要獲取當前數據時間並對其進行格式化 – MethodMan