Question

I'd like to find out how many people did any event (Count of records in EvntFct table).

This provides me with the count for a day - April 1st. I'm using Teradata as my SQl server.

But how would I calculate a daily average since then till yesterday?

SELECT CAST(EVENT_TIMESTAMP AS DATE) theDate,
                 COUNT(DISTINCT USER_ID) 
FROM EventFct
WHERE CAST(EVENT_TIMESTAMP AS DATE) >'2014-05-10'
GROUP BY theDate
ORDER BY theDate
Était-ce utile?

La solution

Seems like you want to calculate an average of the counts, this is an nested aggregate. To achieve this you have to use a Derived Table:

SELECT avg(cnt) 
FROM
 (
   SELECT CAST(EVENT_TIMESTAMP AS DATE) theDate,
          COUNT(DISTINCT USER_ID) as cnt
   FROM EventFct
   WHERE CAST(EVENT_TIMESTAMP AS DATE) >'2014-05-10'
   GROUP BY theDate
 ) as dt

Of course this eliminates the rows for each date, you might want to keep those rows and add the average as an additional column using a Windowed Aggregate Function:

SELECT CAST(EVENT_TIMESTAMP AS DATE) theDate,
       COUNT(DISTINCT USER_ID) as cnt,
       AVG(cnt) OVER () as avgcnt  
FROM EventFct
WHERE CAST(EVENT_TIMESTAMP AS DATE) >'2014-05-10'
GROUP BY theDate

Autres conseils

SELECT AVG(COUNT(*))
FROM EventFct
WHERE CAST(EVENT_TIMESTAMP AS DATE) > '2014-05-10'
GROUP BY USER_ID, CAST(EVENT_TIMESTAMP AS DATE)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top