简体   繁体   中英

Snowflake: In Snowflake SQL how would I select a single field from the users view and put into JSON format?

I'm finding the snowflake documentation very challenging to follow and derive solutions.

I want to select a list of users from snowflake. I only need one column (any account login that matches an email address), and I need to put this into JSON format.

I do not want to select any other field except for 'name' (which in our case is an email address).

This should be easy, and maybe it is, but I cannot find any documentation that would lead to a solution.


When I do a search on selecting users, I am taken to this page

https://docs.snowflake.com/en/sql-reference/sql/show-users.html <== no examples


the command is 'show users;'

The problem with this is that:

  1. I cannot select a single field using that command
  2. I cannot select where like '%@%' (the simplest match for an account that looks like an email address)
  3. I cannot put this into JSON format.

Ideally I would like to do is to select from a view.

With a search, I am led to a 'users view', but I see no examples on that page on how to select from it, and everything I try fails.

https://docs.snowflake.com/en/sql-reference/account-usage/users.html <== how do I select from this view?

I want to select from a view where the login contains '@' and then put into JSON format.


I need JSON output that looks like the following:

{
 "users": [

    {"user": '<email_address>'},
    {"user": '<email_address>'},
    {"user": '<email_address>'},
    ...
    ]
}

Is it even possible to accomplish this?

Any help would be greatly appreciated.

Yes, it is definitily possible to achieve the exact output as requested.

RESULT_SCAN is used to get access of SHOW USERS resultset:

SHOW USERS;

SELECT OBJECT_CONSTRUCT('users', ARRAY_AGG(OBJECT_CONSTRUCT('user', "name")))
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID())) 
WHERE "name" ILIKE '%@%';

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM