预期的结果是字段值按逗号分割,以行的形式展示,然后再group by这些分割出来的字段值,求一个对应的数量,我表达的意思你理解了吗?我把正确的oracle的SQL发给你,你帮我改成pgsql语法的:select t.executor_id, t.EXECUTOR, count(t.executor_id) allcount from( with t as ( select t.executor_id, t.executor from EMP_REPAIR_WORKORDER_BASEINFO t where to_char(t.Create_Date, 'YYYY-MM') = to_char(sysdate, 'YYYY-MM')) select regexp_substr(executor_id, '[^,]+', 1, level) AS executor_id, regexp_substr(executor, '[^,]+', 1, level) executor from t connect by level <= regexp_count(executor_id, '[^,]+') and executor_id = prior executor_id and prior dbms_random.value > 0 ) t group by t.executor_id, t.EXECUTOR
时间: 2023-05-18 15:04:46 浏览: 120
以下是转换后的pgsql语法:
SELECT t.executor_id, t.EXECUTOR, COUNT(t.executor_id) allcount
FROM (
WITH t AS (
SELECT t.executor_id, t.executor
FROM EMP_REPAIR_WORKORDER_BASEINFO t
WHERE to_char(t.Create_Date, 'YYYY-MM') = to_char(now(), 'YYYY-MM')
)
SELECT split_part(executor_id, ',', level) AS executor_id, split_part(executor, ',', level) AS executor
FROM t
CONNECT BY level <= regexp_count(executor_id, '[^,] ')
AND executor_id = PRIOR executor_id
AND PRIOR random() > 0
) t
GROUP BY t.executor_id, t.EXECUTOR;
相关问题
sql中group by某一个个字段,查询出的另一个字段用逗号分割查询出来
可以使用MySQL中的`GROUP_CONCAT`函数来完成这个需求。例如:
```
SELECT column1, GROUP_CONCAT(column2 SEPARATOR ',')
FROM table_name
GROUP BY column1;
```
其中,`column1`是分组依据的字段,`column2`是需要用逗号分割的字段。`GROUP_CONCAT`函数会将每个分组内的`column2`字段值用逗号连接起来,生成一个字符串。`SEPARATOR`参数指定了连接符,这里使用逗号作为分隔符。最终的结果会按照`column1`字段进行分组,每个分组内的`column2`字段值会被用逗号连接成一个字符串。
mysql根据逗号分割字段
如果要根据逗号分割字段,可以使用MySQL内置的函数`SUBSTRING_INDEX()`。
例如,假设我们有一个表`test`,其中有一个字段`tags`存储了多个标签,多个标签之间以逗号隔开,如下所示:
```
+----+---------------------+
| id | tags |
+----+---------------------+
| 1 | mysql, database |
| 2 | php, web development|
| 3 | python, data science|
+----+---------------------+
```
现在我们想要查询出每个标签出现的次数,可以使用如下SQL语句:
```
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(tags, ',', numbers.n), ',', -1) AS tag, COUNT(*) AS count
FROM test
CROSS JOIN
(
SELECT 1 n UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
) numbers
WHERE numbers.n <= 1 + (LENGTH(tags) - LENGTH(REPLACE(tags, ',', '')))
GROUP BY tag
ORDER BY count DESC;
```
这个SQL语句使用了`SUBSTRING_INDEX()`函数和`CROSS JOIN`,将每个标签分离出来,并统计每个标签出现的次数。最终的查询结果如下所示:
```
+-------------+-------+
| tag | count |
+-------------+-------+
| mysql | 1 |
| database | 1 |
| php | 1 |
| web development| 1 |
| python | 1 |
| data science| 1 |
+-------------+-------+
```
这个方法虽然比较繁琐,但是可以实现根据逗号分割字段的功能。
阅读全文