如何使用数据在与单独行相同的列中创建SQL视图?

时间:2019-02-28 17:57:36

标签: mysql sql view pivot-table

我有一个表,可在同一表中保留多个不同的指标。度量的值有一个“值” 列,另一列用于标识度量的实际值。然后,我有一个纪元时间戳列,一个网站列和一个主键ID列。

我正在尝试创建一个MySQL视图以将每个指标显示为单独的列,并让每一行代表每天每个指标的最新值(一天中一个指标可能有多行,我想然后仅显示当天的最新时间值。

编辑:“值”列仅是给定时间的总计,因此这些值均不应相加/相加。对于page_views,该值为从时间开始到现在的总和。

当前表

+----+---------------+--------------+-----------------+-------+
| ID | TIMESTAMP     | WEBSITE      | METRIC_TYPE     | VALUE |
+----+---------------+--------------+-----------------+-------+
| 1  | 1546610400000 | example1.com | page_views      | 101   |
| 2  | 1546610401000 | example1.com | page_views      | 117   |
| 3  | 1546610400000 | example1.com | unique_visitors | 30    |
| 4  | 1546610401000 | example2.com | page_views      | 22    |
| 5  | 1546610401000 | example2.com | ad_referrals    | 8     |
| 6  | 1546681000300 | example1.com | page_views      | 206   |
+----+---------------+--------------+-----------------+-------+

上述摘要的注释:所有时间戳记均为01/04/2019,但第6行除外。我想创建一个SQL查询以将该表转换为以下视图:

所需视图

+------------+--------------+------------+-----------------+--------------+
| DATE       | WEBSITE      | PAGE_VIEWS | UNIQUE_VISITORS | AD_REFERRALS |
+------------+--------------+------------+-----------------+--------------+
| 01/04/2019 | example1.com | 117        | 30              | NULL         |
| 01/04/2019 | example2.com | 22         | NULL            | 8            |
| 1/05/2019  | example1.com | 206        | NULL            | NULL         |
+------------+--------------+------------+-----------------+--------------+

我知道如何使用DATE_FORMAT(FROM_UNIXTIME(floor(timestamp/1000))TIME(FROM_UNIXTIME(floor(timestamp/1000)))将时间戳转换为日期。

我需要透视这些结果,并且每天仅选择每个指标的最新记录。

我已经尝试过多次重新加入该表,但是与我要查找的结果不尽相同(位置是机密数据,因此该数据用于不同的度量标准,但格式相同)。

2 个答案:

答案 0 :(得分:2)

您可以使用条件聚合,但是您需要每天向下过滤到最后一行:

select date(FROM_UNIXTIME(floor(timestamp/1000)) as date, website,
       max(case when metric_type = 'page_views' then value end) as page_views,
       max(case when metric_type = 'unique_visitors' then value end) as unique_visitors,
       max(case when metric_type = 'ad_referrals' then value end) as ad_referrals
from t
where t.timestamp = (select max(t2.timestamp)
                     from t t2
                     where date(FROM_UNIXTIME(floor(t2.timestamp/1000)) = date(FROM_UNIXTIME(floor(t.timestamp/1000)) and
                           t2.website = t.website and
                           t2.metric_type = t.metric_type
                    )
group by date, website;

答案 1 :(得分:1)

您可以将条件聚合用作

select DATE( FROM_UNIXTIME( floor(timestamp/1000) ) ) as date, website,
       sum(case when metric_type = 'page_views' then value end) as page_views,
       sum(case when metric_type = 'unique_visitors' then value end) as unique_visitors,
       sum(case when metric_type = 'ad_referrals' then value end) as ad_referrals
  from yourTable
group by date, website;

+------------+--------------+------------+-----------------+--------------+
| DATE       | WEBSITE      | PAGE_VIEWS | UNIQUE_VISITORS | AD_REFERRALS |
+------------+--------------+------------+-----------------+--------------+
| 01/04/2019 | example1.com | 218        | 30              | NULL         |
| 01/04/2019 | example2.com | 22         | NULL            | 8            |
| 01/05/2019 | example1.com | 206        | NULL            | NULL         |
+------------+--------------+------------+-----------------+--------------+

Demo