下面的3条select语句如何包含在单个mySQL视图中

时间:2019-03-31 04:47:21

标签: mysql view

需要创建一个执行以下3条select语句的视图

我可以在SQL编辑器中执行它们并获得比率。

select @lag := count(twitter_handles) from handle_impressions where today_impressions > yest_impressions;
select @decay := count(twitter_handles) from handle_impressions where today_impressions < yest_impressions;
select @ratio := @lag/@decay;

目的是使单个视图封装3个select语句以返回滞后,衰减和比率

1 个答案:

答案 0 :(得分:2)

我们可以尝试使用条件聚合编写单个查询:

SELECT
    COUNT(CASE WHEN today_impressions > yest_impressions THEN 1 END) AS lag_val,
    COUNT(CASE WHEN today_impressions < yest_impressions THEN 1 END) AS decay_val,
    COUNT(CASE WHEN today_impressions > yest_impressions THEN 1 END) /
        COUNT(CASE WHEN today_impressions < yest_impressions THEN 1 END) AS ratio
FROM handle_impressions;