我有一个非常大的以下查询:
SELECT
DATE(added_on) 'Week Of',
COUNT(*) 'No. Updates',
(CASE WHEN COUNT(*) <= 500 THEN 6.75 WHEN COUNT(*) <= 750
THEN 6.30 WHEN COUNT(*) <= 1000 THEN 6.00 WHEN COUNT(*) <= 1250
THEN 5.50 ELSE 4.60 END
) Rate
Rate * COUNT(*) // HOW TO DO THIS??
FROM
Fox_title
GROUP BY
WEEK(added_on)
ORDER BY
added_on
我如何将COUNT(*)
*我Rate
语句中的CASE
多个?或者我是否必须再次编写CASE语句?
答案 0 :(得分:2)
重复case
或使用子查询:
select t.*, t.Rate * `No. Updates`
from (SELECT DATE(min(added_on)) as `Week Of`, COUNT(*) as `No. Updates`,
(CASE WHEN COUNT(*) <= 500 THEN 6.75
WHEN COUNT(*) <= 750 THEN 6.30
WHEN COUNT(*) <= 1000 THEN 6.00
WHEN COUNT(*) <= 1250 THEN 5.50
ELSE 4.60
END) as Rate
FROM Fox_title
GROUP BY WEEK(added_on)
) t
ORDER BY `Week Of`;
我对您的查询进行了一些其他更改。首先,我将列别名周围的单引号更改为后退标记。通常,单引号应仅用于字符串常量。 Back ticks是用于封装标识符的MySQL方法。
我还将date(addon)
更改为date(min(addon))
。这可确保您获得本周最早的日期。否则,你会得到一个任意的日期。