如何按条件划分列中的 2 个值

How to divide 2 values in a column by condition

我有一个table这样的

metrics                  metrics_value 

pageviews                111
bounces                  222
avgSessionsDuration      100

我需要计算新值,最好不添加新列

  1. avgSessionsDuration / 60 AS 分钟

  2. (bounces/pageviews)*100 AS 跳出率

我想要的输出


metrics                  metrics_value 

pageviews                111
bounces                  222
avgSessionsDuration      100

bounce rate              200
minutes                  1,6

获得分钟数没问题,但是有了第二个新指标,我不知道该怎么做

我做了什么

#  Get minutes

SELECT metrics, metrics_value,
CASE WHEN metrics = 'avgSessionDuration' THEN metrics_value / 60
END
FROM Table

#  Get bounces and pageviews
SELECT bounces / pageviews, 
      (SELECT metrics
      WHERE metrics = 'bounces'
      ) bounces,

      (SELECT metrics
      WHERE dmetrics = 'pageviews'
      ) pageviews

FROM Table Table

感谢任何帮助

您似乎只想 union all 并为每个新指标单独计算::

SELECT metrics, metrics_value
FROM t
UNION ALL
SELECT 'minutes', metrics_value / 60
FROM t
UNION ALL
SELECT 'bounce rate',
       (100* MAX(CASE WHEN metrics = 'bounces' THEN metrics_value END) /
        MAX(CASE WHEN metrics = 'pageviews' THEN metrics_value END)
       )
FROM t;

为您想要的每个值使用子查询:

select metrics, metrics_value  from tablename
union all
select 'bounce rate', 
  100.0 * (select metrics_value from tablename where metrics = 'bounces') /
  (select metrics_value from tablename where metrics = 'pageviews') 
union all
select 'minutes',
  (select metrics_value from tablename where metrics = 'avgSessionsDuration') / 60.0

参见demo
结果:

> metrics             | metrics_value   
> :------------------ | :---------------
> pageviews           | 111
> bounces             | 222
> avgSessionsDuration | 100
> bounce rate         | 200
> minutes             | 1.6

请注意,根据您使用的数据库,您可能需要向最后 2 个 UNIONed 查询添加 FROM 子句,例如:

FROM dual

你可以这样做:

SELECT 
bounces.metrics / pageviews.metrics AS bounceRate
FROM  (SELECT metrics
        WHERE metrics = 'bounces'
      ) bounces,
       (SELECT metrics
         WHERE metrics = 'pageviews'
      ) pageviews

这不是一个优雅的方式,但你可以像这样一步一步地做:

WITH
-- Let's create table same, to shown in your example: 
  metrics_table AS(
  SELECT
    'page_views' AS metrics,
    111 AS metrics_value
  UNION ALL
  SELECT
    'bounces',
    222
  UNION ALL
  SELECT
    'avgSessionsDuration',
    100)

--  get everything from it: 
SELECT
  metrics,
  metrics_value
FROM
  metrics_table

-- unite it with calculation for seconds transformed to minutes
UNION ALL
SELECT
  'minutes',
  metrics_value/60
FROM
  metrics_table
WHERE
  metrics = 'avgSessionsDuration'

-- and finnaly add bounce rate:
UNION ALL
SELECT
  'bounce rate',
  bounces/page_views * 100
FROM (
    -- trick called inline view used here
    SELECT
        metrics_value AS bounces
    FROM
        metrics_table
    WHERE
        metrics = 'bounces') -- bounce
JOIN (
    SELECT
        metrics_value AS page_views
    FROM
         metrics_table
    WHERE
         metrics = 'page_views') -- page views

--we just want to join it in any condition:
ON
  TRUE

如果需要任何说明或进一步的帮助,请随时提出。