# Trying to query to find user custom fields filled out

**URL:** https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472
**Category:** Data & reporting
**Tags:** sql-query, user-custom-fields
**Created:** [April 30, 2018, 6:42pm UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472 "2018-04-30T18:42:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [April 30, 2018, 6:42pm UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472/1 "2018-04-30T18:42:26Z")

</div>

I have a [data explorer](https://meta.discourse.org/t/32566?silent=true) query where I am trying to find all who have filled out `user custom field` 1-4, and would love it to only show those with all 4 as columns for each user, but my postgre skills are _limited_ and I haven’t been able to figure it out:

```
SELECT user_id, name, value
FROM user_custom_fields
WHERE name IN ('user_field_1','user_field_2','user_field_3','user_field_4')
    AND value IS NOT NULL
ORDER BY user_id ASC, name ASC

```

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [April 30, 2018, 11:37pm UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472/3 "2018-04-30T23:37:58Z")

</div>

I’m sure there are better ways of approaching this, but this should work to give you a single row containing all of the fields for each user who has filled out at least one of your user fields.

```plaintext
WITH user_field_1 AS (
SELECT ucf.value,
ucf.user_id
FROM user_custom_fields ucf
WHERE ucf.name = 'user_field_1'
AND ucf.value IS NOT NULL
AND ucf.value != ''
),
user_field_2 AS (
SELECT ucf.value,
ucf.user_id
FROM user_custom_fields ucf
WHERE ucf.name = 'user_field_2'
AND ucf.value IS NOT NULL
AND ucf.value != ''
),
user_field_3 AS (
SELECT ucf.value,
ucf.user_id
FROM user_custom_fields ucf
WHERE ucf.name = 'user_field_3'
AND ucf.value IS NOT NULL
AND ucf.value != ''
),
user_field_4 AS (
SELECT ucf.value,
ucf.user_id
FROM user_custom_fields ucf
WHERE ucf.name = 'user_field_4'
AND ucf.value IS NOT NULL
AND ucf.value != ''
),
target_fields AS (
SELECT
u.id AS user_id,
uf1.value AS user_field_1,
uf2.value AS user_field_2,
uf3.value AS user_field_3,
uf4.value AS user_field_4
FROM users u
LEFT JOIN user_field_1 uf1
ON uf1.user_id = u.id
LEFT JOIN user_field_2 uf2
ON uf2.user_id = u.id
LEFT JOIN user_field_3 uf3
ON uf3.user_id = u.id
LEFT JOIN user_field_4 uf4
ON uf4.user_id = u.id
)

SELECT
*
FROM target_fields tf
WHERE tf.user_field_1 IS NOT NULL
OR tf.user_field_2 IS NOT NULL
OR tf.user_field_3 IS NOT NULL
OR tf.user_field_4 IS NOT NULL
ORDER BY tf.user_id

```

---

<div class="post-metadata">

### Author: ![jerdog](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jerdog/32/122843_2.png) [@jerdog](https://meta.discourse.org/u/jerdog)
#### Post date: [April 30, 2018, 11:53pm UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472/4 "2018-04-30T23:53:40Z")

</div>

If there is a better way I am so ok with trying/doing it! I’ll give this a shot though.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [May 1, 2018, 12:22am UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472/5 "2018-05-01T00:22:53Z")

</div>

By ‘better’ I mean more elegant and more efficient. This should give you the results that you are looking for. I was stuck for a while on how to get the results in a single row for each user. If that isn’t a requirement, the query could be simplified a lot.

---

<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: [June 8, 2024, 12:37pm UTC](https://meta.discourse.org/t/trying-to-query-to-find-user-custom-fields-filled-out/86472/6 "2024-06-08T12:37:33Z")

</div>

This topic was automatically closed after 2230 days. New replies are no longer allowed.
