MySQL - 如何自动化视图查询,从最近的日期减去前一天的指标和日期戳最近的数据
Posted
技术标签:
【中文标题】MySQL - 如何自动化视图查询,从最近的日期减去前一天的指标和日期戳最近的数据【英文标题】:MySQL - How Can I Automate a View Query That Subtracts Metrics From the Most Recent Date To The Previous Day & Date Stamps The Most Recent Data 【发布时间】:2017-12-09 17:06:31 【问题描述】:我有一个每日 API 调用,它为我提供一个唯一 ID 的 LIFETIME 数据,并在每天午夜为数据批次打上日期戳。此数据附加到 mysql 中的数据库中,但我需要将其转换为可以获取每个指标的唯一 ID 的每日数据的方式。
我在 MySQL 中的表:
期望的输出:
对于 id 3,我将前一天(2017 年 7 月 3 日)和最近日期(2017 年 7 月 4 日)的差异用于其各自的指标(展示次数、点击次数),并将该订单项的时间戳记为 7/4/ 2017 年。我需要通过 id 为多个 id 和指标处理这种类型的转换。谢谢!
根据 Jacob 的反馈更新我的查询:
CREATE VIEW `facebook_insights` AS
SELECT
t1.id
, t1.timestamp
, t1.message
, t1.posted
, t1.permalink_url
, t1.caption
, t1.link
, t1.type
, t1.post_impressions - t2.post_impressions as Impressions
, t1.post_impressions_organic - t2.post_impressions_organic as Post_Impressions_Organic
, t1.post_impressions_paid - t2.post_impressions_paid as Post_Impressions_Paid
, t1.post_engaged_users - t2.post_engaged_users as Post_Engaged_Users
, t1.post_consumptions - t2.post_consumptions as Post_Consumptions
, t1.post_negative_feedback - t2.post_negative_feedback as
Post_Negative_Feedback
, t1.post_negative_feedback_unique - t2.Post_Negative_Feedback_Unique as
Post_Negative_Feedback_Unique
, t1.post_impressions_fan - t2.post_impressions_fan as Post_Impressions_Fan
, t1.post_impressions_fan_paid - t2.post_impressions_fan_paid as
Post_Impressions_Fan_Paid
, t1.post_engaged_fan - t2.Post_Engaged_Fan as Post_Engaged_Fan
, t1.post_video_complete_views_organic -
t2.post_video_complete_views_organic as Post_Video_Complete_Views_Organic
, t1.post_video_complete_views_paid - t2.post_video_complete_views_paid as
Post_Video_Complete_Views_Paid
, t1.post_video_views_10s - t2.post_video_views_10s as Post_Video_Views_10s
, t1.post_video_views_10s_unique - t2.post_video_views_10s_unique as
Post_Video_Views_10s_Unique
, t1.post_video_views_organic - t2.post_video_views_organic as
Post_Video_Views_Organic
, t1.post_video_views_paid - t2.post_video_views_paid as
Post_Video_Views_Paid
, t1.post_video_views_clicked_to_play - t2.post_video_views_clicked_to_play
as Post_Video_Views_Clicked_to_Play
FROM
unpaid_media.facebook_insight t1
JOIN unpaid_media.facebook_insight t2
ON t1.id = t2.id
and t1.timestamp = t2.timestamp + INTERVAL 1 DAY
【问题讨论】:
【参考方案1】:这里的关键是将表重新加入到自身中。在下面的查询中,我正在重新加入您的表,但将其设置为时间戳是加入的限定符。这将加入前一天的记录并允许您简单地减去它们。
select
t1.id
, t1.timestamp
, t1.impressions - t2.impressions as impressions
, t1.clicks - t2.clicks as clicks
from
table t1
join table t2
on t1.id = t2.id
and t1.timestamp = t2.timestamp + INTERVAL 1 DAY
【讨论】:
这看起来并不难 @Strawberry: 说起来不是很困难,但不如简单地使用分析函数方便...很多人都难以理解自联接 感谢@Jacobm001,但现在我收到了这个错误:非法混合排序规则(latin1_swedish_ci 隐式)和(utf8_general_ci coercible for operation '=' 我从中调用的表的排序规则设置为 latin1 - 默认排序规则 @UlisesSotomayor:这应该可以解决这个问题 ***.com/a/1008336/1142368【参考方案2】:看起来分组功能很有用,因为您希望按日期和 ID 汇总展示次数和点击次数。此外,您没有提到此数据需要任何连接(还),所以这是假设它在一个表中。下面在 T-SQL 中:
USE DatabaseName
GO
SELECT T.ID,
(CAST(T.TimeStamp) as Date) as Date,
SUM(T.Impressions) as TotalImpressionsPerDay,
SUM(T.Clicks) as TotalClicksPerDay
FROM TableName as T
GROUP BY ID, (CAST(T.TimeStamp) as Date)
ORDER BY ID ASC
【讨论】:
OP 按天计算。他想用它来生成显示每日值的输出。 @Jacobm001 我明白了,区别在于每天的增长。奇怪的是,API 会以这种方式吐出生命周期数据,而不是附加新数据。以上是关于MySQL - 如何自动化视图查询,从最近的日期减去前一天的指标和日期戳最近的数据的主要内容,如果未能解决你的问题,请参考以下文章