# Displaying user custom fields in data explorer query results

**URL:** <https://meta.discourse.org/t/displaying-user-custom-fields-in-data-explorer-query-results/76648>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [12월 22, 2017, 10:48오후 UTC](https://meta.discourse.org/t/displaying-user-custom-fields-in-data-explorer-query-results/76648 "2017-12-22T22:48:03Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)\
**Post date:** [3월 15, 2020, 10:59오전 UTC](https://meta.discourse.org/t/displaying-user-custom-fields-in-data-explorer-query-results/76648/6 "2020-03-15T10:59:46Z")

</div>

> [@LeoMcA](#):
>
> 이걸 자동으로 처리하는 방법이 _있을 수도_ 있지만, 저는 SQL 전문가라고 할 만큼 능숙하지 않아서 어떻게 해야 할지 모르겠네요 😉

저희 사이트에서 이 쿼리가 정말 유용하다는 걸 발견했지만, 그룹별로 작동하도록 하고 이메일 같은 다른 사용자 정보를 포함시키기 위해 커스터마이징/해킹하는 데 꽤 시간이 걸렸어요.

하지만 곧 몇 가지 사용자 필드가 당분간 동적으로 변할 대형 인스턴스를 구축하려고 합니다.

혹시 이 쿼리를 재구성해서 모든 사용자 데이터 필드를 자동으로 출력하게 할 수 있는 분이 계신가요? @simon 님은 어떨까요?

현재 제 쿼리는 다음과 같습니다:

> –[params]  
> – string :group\_name
> 
> With included\_users AS (  
> SELECT  
> gu.user\_id  
> FROM group\_users gu  
> JOIN groups g  
> ON g.id = gu.group\_id  
> WHERE g.name = :group\_name  
> ),  
> e AS (  
> SELECT email, user\_id  
> FROM user\_emails u  
> WHERE u.primary = true  
> )
> 
> SELECT  
> u.id AS user\_id, u.username, u.name, e.email,  
> MAX(CASE ucf.name WHEN ‘user\_field\_6’ THEN value END) AS “Mobile Phone”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_2’ THEN value END) AS “Job Title”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_5’ THEN value END) AS “Employer”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_4’ THEN value END) AS “Postcode”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_14’ THEN value END) AS “Clinical Profession”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_3’ THEN value END) AS “Clinical Speciality”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_17’ THEN value END) AS “Working Clinically”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_1’ THEN value END) AS “Informatics Interest”,  
> MAX(CASE ucf.name WHEN ‘user\_field\_15’ THEN value END) AS “Dietary Requirement”
> 
> FROM user\_custom\_fields AS ucf  
> RIGHT JOIN users AS U on ucf.user\_id = u.id  
> RIGHT JOIN e on ucf.user\_id = e.user\_id  
> WHERE u.id IN (SELECT user\_id FROM included\_users)  
> GROUP BY u.id, e.email  
> ORDER BY username asc

---

_[View the full topic](https://meta.discourse.org/t/displaying-user-custom-fields-in-data-explorer-query-results/76648)._
