0
我有一個日期時間表和倍數屬性,其中我分組了一些我彙總的一些表格,查詢將類似於上週爲每個客戶獲得的收入。 現在我想查看所請求的時間段和前一個時間段之間的更改,以便我將獲得2列收入和previous_revenue。取決於其他字段的彙總值
現在,我正在請求所需時間段的行加上前一個週期的行,對於每個聚合字段,我添加一個case語句,其中返回值或0,如果不在我想要的時間段內。
這會導致與聚合字段一樣多的CASE,但始終使用相同的條件語句。
我不知道是否有這個用例更好的設計...
SELECT
customer,
SUM(
CASE TIMESTAMP_CMP('2016-07-01 00:00:00', ft.date) > 0 WHEN true THEN
REVENUE
ELSE 0 END
) AS revenue,
SUM(
CASE TIMESTAMP_CMP('2016-07-01 00:00:00', ft.date) < 0 WHEN true THEN
REVENUE
ELSE 0 END
) AS previous_revenue
WHERE date_hour >= '2016-06-01 00:00:00'
AND date_hour <= '2016-07-31 23:59:59'
GROUP BY customer
(在我的實際使用情況下,我有很多列,這使得它更加難看)