简体   繁体   English

雪花:在雪花 SQL 中,我将如何从用户视图中看到 select 的单个字段并放入 JSON 格式?

[英]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.我想 select 来自雪花的用户列表。 I only need one column (any account login that matches an email address), and I need to put this into JSON format.我只需要一列(任何与 email 地址匹配的帐户登录),我需要将其放入 JSON 格式。

I do not want to select any other field except for 'name' (which in our case is an email address).我不想 select 除了“名称”(在我们的例子中是 email 地址)之外的任何其他字段。

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 https://docs.snowflake.com/en/sql-reference/sql/show-users.html <== 没有例子


the command is 'show users;'命令是“显示用户;”

The problem with this is that:问题在于:

  1. I cannot select a single field using that command我不能使用该命令 select 单个字段
  2. I cannot select where like '%@%' (the simplest match for an account that looks like an email address)我不能 select 像 '%@%' (对于看起来像 email 地址的帐户的最简单匹配)
  3. I cannot put this into JSON format.我不能把它变成 JSON 格式。

Ideally I would like to do is to select from a view.理想情况下我想做的就是从select来看。

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.通过搜索,我被引导到“用户视图”,但我在该页面上没有看到有关如何从中获取 select 的示例,并且我尝试的一切都失败了。

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

I want to select from a view where the login contains '@' and then put into JSON format.我想从登录包含“@”的视图中输入 select,然后输入 JSON 格式。


I need JSON output that looks like the following:我需要 JSON output ,如下所示:

{
 "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.是的,完全可以按照要求实现精确的 output。

RESULT_SCAN is used to get access of SHOW USERS resultset: RESULT_SCAN用于获取SHOW USERS结果集的访问权限:

SHOW USERS;

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

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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