# Created a script but receiving an error message. Would love some feedback/advice

**URL:** https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [January 15, 2023, 5:38pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842 "2023-01-15T17:38:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Drew-ART](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Drew-ART](https://meta.discourse.org/u/Drew-ART)
#### Post date: [January 15, 2023, 5:38pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842/1 "2023-01-15T17:38:34Z")

</div>

This should probably be a comment in a thread but i couldn’t find the correct one. The cool scripts one seems to be locked now.

The script is designed to pull user\_id, username, name, email, custom\_field1, custom\_field2, trust\_level, visits and a few more stats.

Here’s the code

```plaintext
SELECT
    pr.user_id,
    username,
    name,
    email,
    uf.custom_field1,
    uf.custom_field2,
    tl.trust_level,
    visits,
    COALESCE(topics_viewed, 0) AS topics_viewed,
    COALESCE(posts_read, 0) AS posts_read,
    COALESCE(posts_created, 0) AS posts_created,
    COALESCE(topics_created, 0) AS topics_created,
    COALESCE(topics_with_replies, 0) AS topics_with_replies,
    COALESCE(likes_given, 0) AS likes_given,
    COALESCE(likes_received, 0) AS likes_received
FROM pr
LEFT JOIN tv USING (user_id)
LEFT JOIN pc USING (user_id)
LEFT JOIN tc USING (user_id)
LEFT JOIN twr USING (user_id)
LEFT JOIN lg USING (user_id)
LEFT JOIN e USING (user_id)
LEFT JOIN uf USING (user_id)
LEFT JOIN tl USING (user_id)

```

and here’s the error message i’m getting:

```plaintext
PG::UndefinedTable: ERROR: relation "pr" does not exist
LINE 23: FROM pr
              ^

```

Can anyone see what i’ve missed as i just can’t find it. I’m not skilled in SQL so its been a bit of a struggle getting this far.

Please and thank you

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [January 15, 2023, 6:05pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842/2 "2023-01-15T18:05:39Z")

</div>

The problem is that none of those tables exist. `FROM pr` means to select rows from the table `pr` but there is no table `pr` in Discourse.

Maybe you meant `posts` or `topics` ? Ditto for every other join, none of those tables exist.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [January 15, 2023, 6:55pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842/3 "2023-01-15T18:55:49Z")

</div>

I think it’s been adapted from this post:

> [@User participation](https://meta.discourse.org/t/user-participation/275012):
>
> User participation Shows the following columns over a specified time range (in days): user\_id, username, name, email, visits, topics\_viewed, posts\_read, posts\_created, topics\_created, topics\_with\_replies, likes\_given, likes\_received Full query: -- [params] -- int :from\_days\_ago = 0 -- int :duration\_days = 30 with t as ( select CURRENT\_TIMESTAMP - ((:from\_days\_ago + :duration\_days) \* (INTERVAL '1 days…

But you would need to use the full query @Drew-ART, rather than cherry-picking bits without adapting it to compensate for the missing pieces.

---

<div class="post-metadata">

### Author: ![Drew-ART](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@Drew-ART](https://meta.discourse.org/u/Drew-ART)
#### Post date: [January 16, 2023, 7:52pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842/4 "2023-01-16T19:52:45Z")

</div>

Thank you @Falco and @JammyDodger. Looks like i skipped a big chunk of this. I’ll have another go 🙂

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [February 15, 2023, 7:53pm UTC](https://meta.discourse.org/t/created-a-script-but-receiving-an-error-message-would-love-some-feedback-advice/251842/5 "2023-02-15T19:53:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
