# Add username to two queries

**URL:** <https://meta.discourse.org/t/add-username-to-two-queries/123531>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [July 20, 2019, 6:03pm UTC](https://meta.discourse.org/t/add-username-to-two-queries/123531 "2019-07-20T18:03:07Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![SidV](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sidv/32/119460_2.png) [@SidV](https://meta.discourse.org/u/SidV)\
**Post date:** [July 20, 2019, 6:03pm UTC](https://meta.discourse.org/t/add-username-to-two-queries/123531/1 "2019-07-20T18:03:07Z")

</div>

Hi, I decide to open new topic for this:

> [@(Superseded) What cool data explorer queries have you come up with?](https://meta.discourse.org/t/superseded-what-cool-data-explorer-queries-have-you-come-up-with/43516/222):
>
> Can anyone help me with these two queries? Is there a way to get the username in the csv export instead of the user id?
> 
> - Top quality users in last six months
> - Top 50 posters

### Top quality users in last six months

Well, for the Top quality users in last six months, I used the _[2nd version from query](https://github.com/SidVal/discourse-data-explorer/blob/queries/queries/top-quality-users.sql)_.

And I added the username; and a `:limit` param.

```sql
-- [params]
-- int :limit = 20
SELECT 
    sum(p.score) / count(p) as "average score per post", 
    count(p.id) as post_count, 
    -- p.user_id, 
    u.username
FROM posts p
JOIN users u ON u.id = p.user_id
WHERE p.created_at >= CURRENT_DATE - INTERVAL '6 month'
  AND NOT u.admin
  AND u.active
GROUP by user_id, u.views, u.username
HAVING count(p.id) > 50
ORDER BY sum(p.score) / count(p) DESC
LIMIT :limit

```

### Top 50 posters

For the “Top 50 posters”'s query, I used _[this version](https://github.com/SidVal/discourse-data-explorer/blob/queries/queries/top-50-posters.sql)_, and I added param for limit, and the username you need.

```sql
-- [params]
-- int :months_ago = 1
-- int :limit = 50
WITH query_period AS (
SELECT
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end
),

user_posts_in_period AS (
SELECT
p.user_id, u.username
FROM users u, posts p
INNER JOIN query_period qp
ON p.created_at >= qp.period_start
AND p.created_at <= qp.period_end
WHERE p.user_id > 0
AND u.id = p.user_id
)

SELECT
up.username,
-- up.user_id,
count(1) as post_count
FROM user_posts_in_period up
GROUP BY up.user_id, up.username
ORDER BY post_count DESC
LIMIT :limit

```

Let me know if it works as you want.

Regards

---

_[View the full topic](https://meta.discourse.org/t/add-username-to-two-queries/123531)._
