2017-04-02 136 views
0

我正在創建一個POS數據圖。我創建MySQL子查詢

SELECT DATE_FORMAT(DATE_ADD(DATE(receipts.datenew - INTERVAL (5*60 + 30) MINUTE), INTERVAL (5*60 + 30) MINUTE), '%Y-%m-%d') AS interval_start, 
CONVERT(EXTRACTVALUE(PRODUCTS.ATTRIBUTES, '/properties//entry[@key=\"maincat\"]') USING UTF8) AS MAINCAT, 
ROUND(SUM(TICKETLINES.PRICE * TICKETLINES.UNITS), -3) AS DAYSALES 
FROM TICKETLINES, TICKETS, RECEIPTS, PRODUCTS 
WHERE TICKETLINES.PRODUCT = PRODUCTS.ID 
AND TICKETLINES.TICKET = TICKETS.ID 
AND TICKETS.ID = RECEIPTS.ID 
AND TICKETLINES.PRODUCT IS NOT NULL 
AND (receipts.datenew >= DATE_SUB(NOW(), INTERVAL 3 MONTH) AND receipts.person > 0) 
GROUP BY DATE(receipts.datenew - INTERVAL (5*60 + 30) MINUTE), MAINCAT 
ORDER BY interval_start, MAINCAT 

我也得到了以下數據:

interval_start MAINCAT DAYSALES 
2016-12-31 Drink 45108000 
2016-12-31 Food 49791000 
2016-12-31 Other 109000 
2017-01-01 Drink 14226000 
2017-01-01 Food 27425000 
2017-01-01 Other 36000 
2017-01-02 Drink 20400000 
2017-01-02 Food 25648000 
2017-01-02 Other 109000 

但我需要這種格式:

interval_start Drink  Food 
2016-12-31 45108000 36000 
2016-12-31 49791000 20400000 
2016-12-31 109000  25648000 
2017-01-01 27425000 109000 
2017-01-01 14226000 49791000 

我相信一個子查詢的答案,但我不能圖出來。

在此先感謝。

艾倫

+0

我不明白你如何到達在預期的產出。我想你應該包括一個解釋。 –

+0

我認爲他希望得到樞軸的結果 – Cherif

+0

謝謝你的回覆,我應該更清楚。我正在試圖創建一個圖表,並且供應商聲明要生成折線圖,數據需要具有這些列。 TICKETLINES表包含銷售項目的價格,銷售日期在RECEIPTS中。我的目標是獲得每種銷售物品每天的銷售數據。 雖然我可能會做這個完全錯誤的。 –

回答

0

我已經能夠獲得與下面的查詢的正確格式的數據 - 謝謝你的指點我朝着正確的方向:

SELECT DATE_FORMAT(DATE_ADD(DATE(receipts.datenew - INTERVAL (5*60 + 30) MINUTE), INTERVAL (5*60 + 30) MINUTE), '%Y-%m-%d') AS DATEofSALES, 
SUM(CASE WHEN CONVERT(EXTRACTVALUE(PRODUCTS.ATTRIBUTES, '/properties//entry[@key=\"maincat\"]') USING UTF8) = 'Drink' THEN ticketlines.price ELSE 0 END) DRINKSALES, 
SUM(CASE WHEN CONVERT(EXTRACTVALUE(PRODUCTS.ATTRIBUTES, '/properties//entry[@key=\"maincat\"]') USING UTF8) = 'Food' THEN ticketlines.price ELSE 0 END) FOODSALES, 
SUM(CASE WHEN CONVERT(EXTRACTVALUE(PRODUCTS.ATTRIBUTES, '/properties//entry[@key=\"maincat\"]') USING UTF8) = 'Other' THEN ticketlines.price ELSE 0 END) OTHERSALES 
FROM TICKETLINES, TICKETS, RECEIPTS, PRODUCTS 
WHERE TICKETLINES.PRODUCT = PRODUCTS.ID 
AND TICKETLINES.TICKET = TICKETS.ID 
AND TICKETS.ID = RECEIPTS.ID 
AND TICKETLINES.PRODUCT IS NOT NULL 
AND (receipts.datenew >= {ts '2017-02-01 06:00:00.000'} AND receipts.datenew < {ts '2017-03-31 14:28:00.000'} AND receipts.person > 0) 
GROUP BY DATE(receipts.datenew - INTERVAL (5*60 + 30) MINUTE) ORDER BY DATEofSALES