我正在使用 MySQL 5.0。
我想根据视图中的前一行执行计算。这可能吗?我假设不是因为在查询实际运行之前视图不存在,所以是否有解决方法,或者创建和更新表是我最好的选择?
这是我不正确的编码,缺乏逻辑,我知道,但它显示了我想要完成的事情:
CREATE VIEW `Master Facebook Data` tt1 AS SELECT
t1.id AS `Client ID`,
t1.name AS `Client Name`,
t1.`Date`,
avg(t1.`Daily People Talking About This`) AS `Avg Daily Talk`,
(((tt1.`Avg Daily Talk`-IFNULL(tt2.`Avg Daily Talk`, 0)))/tt1.`Avg Daily Talk`) AS `Change in Talk`,
avg(t1.`Weekly Total Reach`) AS `Avg Weekly Reach`,
(((tt1.`Avg Weekly Reach`-IFNULL(tt2.`Avg Weekly Reach`, 0)))/tt1.`Avg Weekly Reach`) AS `Change in Reach`,
avg(t1.`Daily Viral Reach`) AS `Avg Daily Viral`,
(((tt1.`Avg Daily Viral`-IFNULL(tt2.`Avg Daily Viral`, 0)))/tt1.`Avg Daily Viral` AS `Change in Viral`
FROM `clients` t1
JOIN `FB Exports - Key Metrics` t2
ON t1.`id` = t2.`client`
LEFT OUTER JOIN `Master Facebook Data` tt2
ON tt1.`Client ID` = tt2.`Client ID`
AND month(tt1.`Date`)-month(tt2.`Date`)=1
GROUP BY t2.`client`, month(t2.`Date`)
ORDER BY t2.`client`, t2.`Date`
非常感谢!=]