简体   繁体   English

从SQL中的多数据列中提取数据

[英]Extracting Data from a Multi-Data Column in SQL

I'm creating a sales leaderboard in HOLISTICS and the column " user_id " is a multi-data column. 我正在HOLISTICS中创建一个销售排行榜,而“ user_id ”列是一个多数据列。

Here's a snapshot of the column "user_id": 这是“ user_id”列的快照:

在此处输入图片说明

I need to show the "name" part of the user. 我需要显示用户的“名称”部分。 I tried using CONVERT and even JSON_VALUE but both are not recognized by Holistics. 我尝试使用CONVERT甚至JSON_VALUE,但Holistics均无法识别。

I used CAST but still the user_id is in numerical form. 我使用了CAST,但user_id仍然是数字形式。

Here's the my code: 这是我的代码:

在此处输入图片说明

And here's the data output: 这是数据输出:

在此处输入图片说明

Can you help me on what to do to be able to show the actual name of the sales person? 您能帮我做什么才能显示销售人员的真实姓名吗?


I'm a newbie here and its my first post that's why all my snipshots are put in a link form. 我是这里的新手,也是我的第一篇文章,这就是为什么我所有的片段都以链接形式放置的原因。

To select a particular field from a JSON data (and JSON is what you have in user_id column), try this combination: 要从JSON数据中选择特定字段(而JSON是user_id列中的内容),请尝试以下组合:

SELECT 
  JSON_UNQUOTE(JSON_EXTRACT(user_id,'$.id')) as id
  JSON_UNQUOTE(JSON_EXTRACT(user_id,'$.name')) as user_name
FROM public.deals

This should return the user's id and name from your JSON column. 这应该从您的JSON列返回用户的ID和名称。

Whatever software you use, it probably expects the data to be retrieved in a row-column format , so you just need to play with the SQL query, so that it returns properly formatted data. 无论使用哪种软件,它都可能希望以行列格式检索数据,因此您只需要使用SQL查询,以便它返回格式正确的数据。 And since you have JSONs in a user_id column (which seems weird, but nevermind) - a combination of JSON_EXTRACT, JSON_UNQUOTE and perhaps CAST should do the trick. 而且,由于在user_id列中有JSON(这看起来很奇怪,但是没关系)-JSON_EXTRACT,JSON_UNQUOTE和CAST的组合应该可以解决问题。

But bear in mind, that running DISTINCT on a big table using those methods could be slow. 但是请记住,使用这些方法在大表上运行DISTINCT可能很慢。

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

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