显示在Grafana中逗号分开的列值

发布于 2025-01-20 02:31:49 字数 453 浏览 5 评论 0原文

我有一个包含 2 列的表,

organization_id | user_name
1 | abc
1 | xyz
2 | bhi
2 | ipq
2 | sko
3 | ask
...

每个组织可以拥有任意数量的用户,范围从 1 到 100、2000 等。 我想在 grafana 中的表格中显示它们,如下所示:

organization_id | user_name
1 | abc, xyz
2 | bhi, ipq, sko
3 | ask

由于可能有很多用户,我想显示属于同一组织的任意 10 个用户。

这里的数据库是timescale db,该表也是一个时间序列表,显示用户何时注册

I have a table with 2 columns

organization_id | user_name
1 | abc
1 | xyz
2 | bhi
2 | ipq
2 | sko
3 | ask
...

Each organization could have any number of users ranging from 1 to 100, 2000 and so on.
I wanted to show them in grafana in a table as following:

organization_id | user_name
1 | abc, xyz
2 | bhi, ipq, sko
3 | ask

Since there could be many users I want to show any 10 users belonging to same organization.

The database here is timescale db, the table is also a time series table showing when user was registered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2025-01-27 02:31:49

如果我正确理解您希望每个组织有 10 个用户,您可以使用下面的查询。
我在 CTE 中添加了 group by 以避免返回重复的用户名。
在测试架构中,组织 2 存在重复的“pqr”值,但该用户名仅返回一次,即使 2 的用户名少于 10 个
测试架构 db Fiddle 此处

With topTen as
(Select
  Organisation_id,
  User_name,
  Rank() over (
    partition by organisation_id 
    order by user_name) rn
From table_name 
group by 
  Organisation_id,
  user_name)
Select
  Organisation_id,
  String_agg(user_name,',') users
From topTen
Where rn <= 10
group by Organisation_id;
组织 ID |用户                                  
--------------: | :--------------------------------------
              1 | abc、abk、def、ghi、jkl、mno、pqr、rst、ruk、stu
              2 | abk、pqr、rst、ruk、stu、vwx                

另一种可能有用的替代方案。如果删除 where 并将以下内容放在 From topTen 之后,您将获得所有不同的用户名,每行 10 个。

group by Organisation_id,rn/10
order by Organisation_id,rn/10;

db<>fiddle 此处

If I understand rightly that you want 10 users per organisation you can use the query below.
I have added group by in the CTE to avoid returning duplicate user_name's.
In the test schema there are duplicate values of 'pqr' for organisation 2 but this username is only returned once even though there are less then 10 user_name's for 2
test schema db Fiddle here

With topTen as
(Select
  Organisation_id,
  User_name,
  Rank() over (
    partition by organisation_id 
    order by user_name) rn
From table_name 
group by 
  Organisation_id,
  user_name)
Select
  Organisation_id,
  String_agg(user_name,',') users
From topTen
Where rn <= 10
group by Organisation_id;
organisation_id | users                                  
--------------: | :--------------------------------------
              1 | abc,abk,def,ghi,jkl,mno,pqr,rst,ruk,stu
              2 | abk,pqr,rst,ruk,stu,vwx                

Another alternative which may be useful. If you remove the where and put the following after From topTen you will get all the distinct user_names, 10 per row.

group by Organisation_id,rn/10
order by Organisation_id,rn/10;

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文