gpt4 book ai didi

MySQL - 我如何自动化一个 View 查询,该查询从最近的日期减去指标到前一天和日期戳最新的数据

转载 作者:可可西里 更新时间:2023-11-01 08:09:36 25 4
gpt4 key购买 nike

我有一个每日 API 调用,它为我提供一个唯一 ID 的 LIFETIME 数据,并在每天午夜为数据批处理添加日期戳。此数据附加到 MySQL 中的数据库,但我需要以一种可以获取每个指标的唯一 ID 的每日数据的方式对其进行转换。

我在 MySQL 中的表:

enter image description here

期望的输出:

enter image description here

对于 id 3,我将前一天 (7/3/2017) 和最近日期 (7/4/2017) 的各自指标(展示次数、点击次数)和时间戳记为 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

最佳答案

这里的关键是重新加入表本身。在下面的查询中,我重新加入你的表,但设置它以便时间戳是连接的限定符。这将加入前一天的记录,并允许您简单地减去它们。

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

关于MySQL - 我如何自动化一个 View 查询,该查询从最近的日期减去指标到前一天和日期戳最新的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44936232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com