Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to realized YOY/MOM on druid? #7928

Closed
xqwbx163 opened this issue Jun 20, 2019 · 2 comments
Closed

how to realized YOY/MOM on druid? #7928

xqwbx163 opened this issue Jun 20, 2019 · 2 comments

Comments

@xqwbx163
Copy link

how to realize YOY or MOM on druid sql (or json file) ? just like this : can you help me ?

Month X – Month Y numbers
_______________________ x 100 = Percentage Growth
Month X

Year X – Year Y numbers
_______________________ x 100 = Percentage Growth
Year X number

@vogievetsky
Copy link
Contributor

This is not Druid specific (more like general SQL) but you would do:

SELECT
  COUNT(*) AS "BothMonths",
  COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00') AS "MonthX",
  COUNT(*) FILTER(WHERE TIMESTAMP '2019-05-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00') AS "MonthY",
  (
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-05-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00') -
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00')
  ) / 
  CAST(
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00')
  AS DOUBLE)
  * 100 AS "MonthOverMonth"
FROM "wikiticker"
WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00'

Or (with a grouping):

SELECT
  "channel",
  COUNT(*) AS "BothMonths",
  COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00') AS "MonthX",
  COUNT(*) FILTER(WHERE TIMESTAMP '2019-05-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00') AS "MonthY",
  (
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-05-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00') -
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00')
  ) / 
  CAST(
    COUNT(*) FILTER(WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-05-01 00:00:00')
  AS DOUBLE)
  * 100 AS "MonthOverMonth"
FROM "wikiticker"
WHERE TIMESTAMP '2019-04-01 00:00:00' <= "__time" AND "__time" < TIMESTAMP '2019-06-01 00:00:00'
GROUP BY "channel"
ORDER BY "MonthOverMonth" DESC

image

Obviously you do not need all those aggregates, I am just demonstrating.

The idea is that you filter the overall query to both time intervals and then filter each aggregate to a specific interval

@xqwbx163
Copy link
Author

thanks for you help,i try to accord to your method, it really succeeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants