您好我試圖找到同一列的多個值的平均值之間的差異。Mysql - 選擇同一列的平均值差異
這是模式。
CREATE TABLE `product_priceDiff` (
`mark` char(15) NOT NULL,
`markId` int(11) NOT NULL,
`found_date` date DEFAULT '0000-00-00',
`found_price` decimal(15,3) DEFAULT NULL,
`confirmation_date` date DEFAULT '0000-00-00',
`confirmed_price` decimal(15,3) DEFAULT NULL,
`price_difference` decimal(15,3) DEFAULT NULL,
`action` char(30) DEFAULT NULL,
PRIMARY KEY (`mark`,`markId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
這裏有一些樣本值
+------------------+----------+------------+-------------+-------------------+-----------------+------------------+--------+
| mark | markId | found_date | found_price | confirmation_date | confirmed_price | price_difference | action |
+------------------+----------+------------+-------------+-------------------+-----------------+------------------+--------+
| soap | 6 | 2014-01-13 | 0.410 | 2014-01-15 | 0.420 | 2.439 | BUY |
| lotion | 7 | 2013-09-13 | 0.000 | 2013-09-13 | 0.170 | 0.000 | BUY |
| shaving_cream | 8 | 2014-01-09 | 41.500 | 2014-01-10 | 42.000 | 1.205 | BUY | |
| hairgel | 19 | 2014-01-13 | 8.220 | 2014-01-16 | 8.190 | -0.365 | SELL |
| aftershaves | 20 | 2011-07-12 | 0.000 | 2011-07-12 | 7.500 | 0.000 | SELL |
| shampoo | 21 | 2014-01-14 | 46.870 | 2014-01-17 | 46.480 | -0.832 | SELL |
+------------------+----------+------------+-------------+-------------------+-----------------+------------------+--------+
我想找到的是平均價格的差異。其計算公式爲
Average(price_difference) @ BUY - Average(price_difference) @ SELL
我嘗試使用此查詢這樣做,但它並沒有在所有
SELECT
AVG(CASE WHEN `action` = "BUY" THEN `price_difference`)
- AVG(CASE WHEN `action` = "SELL" THEN `price_difference`)
FROM `product_priceDiff`;
工作,這給了我一個錯誤
ERROR 1064(42000):您在您的SQL語法中有錯誤;檢查對應於你的MySQL服務器版本的權利 語法使用附近的 手冊「行動=‘買入’,然後price_difference) - AVG(情況 當行動=‘沽售’,那麼價格」在1號線
請我應該怎麼做才能得到所需的值提供任何建議感謝,提前
Maxx的
你youst miss'END'statement:'CASE WHEN THEN END' –
Melon
是的,這工作現在錯誤消失。 – Maxx