按日期和计数分组

时间:2019-07-26 12:28:08

标签: sql oracle

我需要创建一个service name countdateSELECT distinct REGEXP_REPLACE(k.rest_Action, '\d+', '*'), count(*), trunc(m.CREATED_TS) FROM MESSAGE m INNER JOIN REST_MESSAGE k ON m.id = k.id WHERE m.CREATED_TS > sysdate - 30 GROUP BY REGEXP_REPLACE(k.rest_Action, '\d+', '*'), trunc(m.CREATED_TS) 的摘要。我无法创建所需的结果。

两个表联接在一起;消息和rest_message

REGEXP_REPLACE(k.rest_Action, '\d+', '*') | COUNT(*) | trunc(m.CREATED_TS)
name1 | 1 | 2018-08-07
name2 | 2 | 2018-08-07
name1 | 3 | 2018-08-06
name2 | 4 | 2018-08-06
name1 | 5 | 2018-08-05

结果如下:

REGEXP_REPLACE(k.rest_Action, '\d+', '*') | 2018-08-07 | 2018-08-06 | 2018-08-05
name1 | 1 | 3 | 5 
name2 | 2 | 4 | null

我想要这个结果:

{
    "id": 102,
    "userName": "figen",
    "email": 3,
    "recipes":[5,6,7]  
}

1 个答案:

答案 0 :(得分:0)

您可以使用大小写透视方法-

SELECT REGEXP_REPLACE(k.rest_Action, '\d+', '*')
      ,CASE WHEN trunc(m.CREATED_TS) = '2018-08-07' THEN CREATED_TS ELSE NULL END AS '2018-08-07'
      ,CASE WHEN trunc(m.CREATED_TS) = '2018-08-06' THEN CREATED_TS ELSE NULL END AS '2018-08-06'
      ,CASE WHEN trunc(m.CREATED_TS) = '2018-08-05' THEN CREATED_TS ELSE NULL END AS '2018-08-05'
FROM MESSAGE m
INNER JOIN REST_MESSAGE k
ON m.id = k.id
WHERE m.CREATED_TS > sysdate - 30
GROUP BY REGEXP_REPLACE(k.rest_Action, '\d+', '*')