,可以幫助的第一件事是正確的SQL的格式化:
SELECT
(CASE
WHEN pickedup = '1' THEN DATE(pickeduptime)
ELSE DATE(ordertime)
END) AS x,
COUNT(productid) AS y,
SUM(CASE
WHEN pickedup = '1' THEN 1
ELSE 0
END) AS z
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW())
GROUP BY x
ORDER BY x ASC
現在我需要找出你正在嘗試做什麼。這是真正的問題開始的地方。目前還不清楚你是否重複使用表格中的行來獲取皮卡。對不起,我幫不了你。你的SQL雖然很奇怪。對於這樣簡單的事情,您不需要使用兩個CASE
命令。
我也無法理解你的SQL,按照來自兩列的日期分組嗎?
經過仔細閱讀,我想我開始明白你想要什麼。你想知道每個日期有多少產品被訂購和取貨。我不會試圖用一個SQL命令來做到這一點,而是使用兩個。兩行,兩條SQL命令。 :-)
首先簡單:有多少種產品,其中有序,分組和按日期排序:
SELECT
DATE(ordertime) as orderdate,
COUNT(productid) AS quantity
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW())
GROUP BY orderdate
ORDER BY orderdate ASC
現在還沒有拿起訂購的產品:
SELECT
DATE(ordertime) as orderdate,
COUNT(productid) AS quantity
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW()) AND
pickedup != '1'
GROUP BY orderdate
ORDER BY orderdate ASC
和現在已經拿起產品,還是上排序訂單日期:
SELECT
DATE(ordertime) as orderdate,
COUNT(productid) AS quantity
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW()) AND
pickedup = '1'
GROUP BY orderdate
ORDER BY orderdate ASC
而這裏的同排序日期:
SELECT
DATE(pickeduptime) as pickedupdate,
COUNT(productid) AS quantity
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW()) AND
pickedup = '1'
GROUP BY pickedupdate
ORDER BY pickedupdate ASC
現在,您可以使用其中任何一種方法在圖表中繪製一條線。
如果你堅持一個查詢,你可以用這一個:
SELECT
DATE(ordertime) as orderdate,
SUM(pickedup) AS pickedup_quantity,
SUM(1-pickedup) AS not_pickedup_quantity
FROM
order_detail
WHERE
productid = '127' AND
YEAR(ordertime) = YEAR(NOW())
GROUP BY orderdate
ORDER BY orderdate ASC
注意,一切必須由一個日期排序。
請顯示您的表格,colums的含義,並告訴我們您要製作的圖表。 – 2014-10-20 09:40:23