我在SQLite中有以下sql语句:
SELECT
...
CASE WHEN facebook_post_audience >= 10000 THEN 15
WHEN facebook_post_audience >= 3000 AND facebook_post_audience <= 9999 THEN 10
WHEN facebook_post_audience >= 1000 AND facebook_post_audience <= 2999 THEN 7.5
WHEN facebook_post_audience >= 100 AND facebook_post_audience <= 999 THEN 5
ELSE 0 END 'Fb reward category',
facebook_post_number 'No. of FB posts/shares',
'Fb reward category' * facebook_post_number 'FB reward',
...
FROM table;
'FB奖励'应该显示先前定义的列的倍数,但无论这些列中的数字是什么,它都只显示零。此外,typeof('Fb奖励类别')表示“文本”,尽管那里只有数字。我今天喝了太多咖啡还是我错过了什么?
答案 0 :(得分:0)
将整个案例陈述放入乘法点:
SELECT
...
CASE WHEN facebook_post_audience >= 10000 THEN 15
WHEN facebook_post_audience >= 3000 AND facebook_post_audience <= 9999 THEN 10
WHEN facebook_post_audience >= 1000 AND facebook_post_audience <= 2999 THEN 7.5
WHEN facebook_post_audience >= 100 AND facebook_post_audience <= 999 THEN 5
ELSE 0 END 'Fb reward category',
facebook_post_number 'No. of FB posts/shares',
CASE WHEN facebook_post_audience >= 10000 THEN 15
WHEN facebook_post_audience >= 3000 AND facebook_post_audience <= 9999 THEN 10
WHEN facebook_post_audience >= 1000 AND facebook_post_audience <= 2999 THEN 7.5
WHEN facebook_post_audience >= 100 AND facebook_post_audience <= 999 THEN 5
ELSE 0 END * facebook_post_number 'FB reward',
...
FROM table
答案 1 :(得分:0)
使用子查询或CTE。而且,当你在它的时候,给你的列别名给出不需要转义的合理名称:
SELECT ...,
facebook_post_number,
fb_reward_category* facebook_post_number as rb_reward,
...
FROM (SELECT t.*,
(CASE WHEN facebook_post_audience >= 10000 THEN 15
WHEN facebook_post_audience >= 3000 THEN 10
WHEN facebook_post_audience >= 1000 THEN 7.5
WHEN facebook_post_audience >= 100 THEN 5
ELSE 0
END) as FB_reward_category
FROM table t
) t
我还简化了case
表达式,因此更容易阅读和维护。