Sql转换'这个'查询结果

时间:2016-03-14 01:54:04

标签: sql oracle

我试图进行查询,这将取代小时(当它的23位将被输出"从23到00"),但我不想制作重复的代码,所以试图找到所有其他时间(从00到22)的解决方案。 在我看来,这听起来像是(采取'这个'输出' FROM' + this +' TO' + this + 1) 有可能吗?

请帮助我为每个用户计算columnt(CNT),我只计算所有的消息,而不是每个用户(我想我应该使用正确的连接?)

结构看起来像这样:

enter image description here

我的代码是:

SELECT a.C_NAME as abonent, 
CASE
WHEN EXTRACT(HOUR FROM C_EXEC_TIME) = 23 THEN 'FROM 23 TO 00'
WHEN EXTRACT(HOUR FROM C_EXEC_TIME) IN (1,3,5,13) THEN 'FROM H TO H+1'
END HH24,
count(C_ST) as CNT
FROM ST_ABONENTS a, QRY_QUEUE qq
WHERE C_EXEC_TIME IS NOT NULL
GROUP BY a.c_NAME, EXTRACT(HOUR FROM C_EXEC_TIME)
ORDER BY a.c_name;

我的结果现在:

enter image description here

它显示ABONENT,HOUR(当这个abonent有活动时),COUNT(消息,现在它显示所有abonents每小时的所有消息)

期望的结果:

enter image description here

数据样本:

enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

如果有其他权利

SELECT a.C_NAME as abonent, 
CASE
WHEN EXTRACT(HOUR FROM C_EXEC_TIME) = 23 THEN 'FROM 23 TO 00'
ELSE 'FROM H TO H+1'
END HH24,
count(C_ST) as CNT
FROM ST_ABONENTS a, QRY_QUEUE qq
WHERE C_EXEC_TIME IS NOT NULL
GROUP BY 
a.c_NAME, 
CASE
WHEN EXTRACT(HOUR FROM C_EXEC_TIME) = 23 THEN 'FROM 23 TO 00'
ELSE 'FROM H TO H+1'
END
ORDER BY a.c_name;

答案 1 :(得分:0)

解决方案是:

SELECT a.C_NAME as abonent, 
CASE
WHEN EXTRACT(HOUR FROM C_EXEC_TIME) = 23 THEN 'С 23 до 00'
ELSE concat(concat('С ',TO_CHAR(EXTRACT(HOUR FROM C_EXEC_TIME))),concat(' до ', TO_CHAR(EXTRACT(HOUR FROM C_EXEC_TIME)+1)))
END HH24,
round(count(C_ST)/60/60*10,2) as CNT
FROM ST_ABONENTS a
inner join qry_type qt on a.id = qt.c_ab_ref
inner join qry_queue qq on qt.id = qq.c_qry_type
WHERE C_EXEC_TIME IS NOT NULL
GROUP BY a.c_NAME,  EXTRACT(HOUR FROM C_EXEC_TIME)
ORDER BY a.c_name, CNT desc;