# Complex user activity report requirement

**URL:** <https://meta.discourse.org/t/complex-user-activity-report-requirement/198641>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [July 30, 2021, 1:47am UTC](https://meta.discourse.org/t/complex-user-activity-report-requirement/198641 "2021-07-30T01:47:00Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![Paul\_King](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paul_king/32/165426_2.png) [@Paul\_King](https://meta.discourse.org/u/Paul_King)\
**Post date:** [August 5, 2021, 2:57pm UTC](https://meta.discourse.org/t/complex-user-activity-report-requirement/198641/21 "2021-08-05T14:57:40Z")

</div>

Thank you so much for that!

The custom user profile field I am trying to use is ‘NZRAB number’

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/1/f19cd05d1b694688992f37c9bc98d62c6bd34474.png)

I had a go, with the resulting script below, but obviously got it wrong, as this gave ‘Undefined function’ error

I was not sure if field name is supposed to exclude capital letters or spaces - the Editor did not seem to like the space in the fieldname at least, so I swapped in an underscore

I might also be conflating the field name with the field label - but if so I am not sure how to tell what field name belongs to the label ‘NZRAB number’

```plaintext
-- coverage: 'week', 'all', or 'date'
-- [params]
-- date :start_date = 2019-08-27
-- date :end_date = 2019-09-30
-- text :coverage = week

WITH date_range AS (
SELECT date_trunc('day', dd):: date AS "date", EXTRACT(week from date_trunc('day', dd):: date) AS "week"
FROM generate_series
        ( :start_date::timestamp 
        , :end_date::timestamp
        , '1 day'::interval) dd
), likes_given AS (
SELECT u.id, dr.date, dr.week, count(pa.*) AS "likes"
FROM date_range dr
FULL JOIN users u ON (1=1)
LEFT JOIN post_actions pa ON (pa.created_at:: date = dr.date and post_action_type_id=2 AND user_id = u.id)
GROUP BY dr.date, dr.week, u.id
ORDER BY u.id, dr.date
), posts_summary AS (
SELECT u.id, u.username, u.created_at, dr.*, count(p.id) - count(t.id) AS replies, count(t.id) AS topics, COALESCE(sum(p.like_count),0) AS likes_received, cf.value as NZRAB_number
from date_range dr
FULL OUTER JOIN users u ON (1=1)
LEFT JOIN posts p ON (p.user_id = u.id AND p.created_at::date=dr.date AND p.deleted_at IS NULL)
LEFT JOIN topics t ON (t.user_id = u.id AND t.created_at::date = dr.date AND p.topic_id = t.id AND t.deleted_at IS NULL)
LEFT JOIN user_custom_fields cf ON (u.id == cf.user_id and cf.name == "NZRAB number") 
GROUP BY u.id, dr.date, dr.week
ORDER BY u.id, dr.date
)
SELECT ps.id, ps.username, ps.created_at, ps.NZRAB_number, CASE
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END AS period, sum(ps.replies) AS replies, sum(ps.topics) AS topics, sum(ps.likes_received) AS likes_received, sum(lg.likes) AS likes_given, COALESCE(sum(posts_read),0) AS posts_read, COALESCE(sum(time_read),0) AS time_read, SUM(visits) AS visits
FROM posts_summary ps

LEFT JOIN likes_given lg ON (ps.date = lg.date AND ps.id = lg.id)
LEFT JOIN visits v ON (v.id = ps.id AND v.date = ps.date)
GROUP BY ps.id, ps.username, ps.created_at, CASE 
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END  
ORDER BY ps.id, CASE
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END

```

resulting error:

> PG::UndefinedFunction: ERROR: operator does not exist: integer == integer  
> LINE 32: LEFT JOIN user\_custom\_fields cf ON (u.id == cf.user\_id and c…  
> ^  
> HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

If you concluded from this that I have not a clue what I am doing, you would be right!

---

_[View the full topic](https://meta.discourse.org/t/complex-user-activity-report-requirement/198641)._
