存储过程或SQL每天执行1-2次 - Oracle

时间:2017-08-17 02:11:35

标签: sql oracle stored-procedures decode dbms-scheduler

我使用一个带有表情符号列的巨大oracle表(可能的值为1,2,3,4,5)呈现报告。我正在为当前的每个报告查询执行实时计数(*)和每个表情符号的百分比计数。我可以选择生成7天,30天,60天和90天的报告。

我的问: - 运行存储过程/ SQL,将每个表情符号的计数和百分比计数存储在一个单独的表中,每天1-2次。 - 如果是新的则插入,将其更新为现有。每天将插入/更新4条记录,频率分别为7,30,60和90天。 - 每天早上6点和/或下午6点通过调度程序运行。

我的架构

desc reports;

MESSAGE_ID NOT NULL NUMBER(10)    
USER_ID    NOT NULL VARCHAR2(10)  
EMOJI      NOT NULL NUMBER(2)     
MESSAGE             VARCHAR2(140) 
TS_CREATE  NOT NULL TIMESTAMP(0)  
TS_UPDATE  NOT NULL TIMESTAMP(0)

我目前从java程序查询30天和60天的频率:

select emoji, COUNT(1) Total_emoji_count, 
ROUND(RATIO_TO_REPORT(COUNT(1)) OVER() * 100,2) Total_emoji_percent
FROM reports where ts_create > sysdate - 30 GROUP BY emoji ORDER BY emoji;

select emoji, COUNT(1) Total_emoji_count, 
ROUND(RATIO_TO_REPORT(COUNT(1)) OVER() * 100,2) Total_emoji_percent
FROM reports where ts_create > sysdate - 60 GROUP BY emoji ORDER BY emoji;

返回: enter image description here

但是,我希望将行显示为列以便于阅读。每天插入这样的东西。

enter image description here

我尝试了以下SQL,我可以获得计数,但无法获得有效的百分比。有什么指针吗?

SELECT SUM(DECODE(emoji, 1, count(1), 0)) "e1_count",
SUM(DECODE(emoji, 2, count(1), 0)) "e2_count",  
SUM(DECODE(emoji, 3, count(1), 0)) "e3_count",  
SUM(DECODE(emoji, 4, count(1), 0)) "e4_count",  
SUM(DECODE(emoji, 5, count(1), 0)) "e5_count"
FROM  reports where ts_create > trunc(sysdate) - 30
GROUP BY emoji ORDER BY emoji;

编辑 - 新变种

emoji_FREQ as (
select
trunc(sysdate),
f.FREQUENCY,
m.emoji,
COUNT(1) e_count,
RATIO_TO_REPORT(COUNT(1)) OVER() * 100 e_percent
FROM reports m, FREQUENCY_TBL f
where m.ts_create > trunc(sysdate) - f.FREQUENCY
GROUP BY FREQUENCY, emoji
) SELECT
trunc(sysdate),
FREQUENCY,
SUM(DECODE(emoji, 1, e_count, 0)) e1_count,
SUM(DECODE(emoji, 2, e_count, 0)) e2_count,
SUM(DECODE(emoji, 3, e_count, 0)) e3_count,
SUM(DECODE(emoji, 4, e_count, 0)) e4_count,
SUM(DECODE(emoji, 5, e_count, 0)) e5_count,
SUM(DECODE(emoji, 1, e_percent, 0)) e1_percent,
SUM(DECODE(emoji, 2, e_percent, 0)) e2_percent,
SUM(DECODE(emoji, 3, e_percent, 0)) e3_percent,
SUM(DECODE(emoji, 4, e_percent, 0)) e4_percent,
SUM(DECODE(emoji, 5, e_percent, 0)) e5_percent
FROM emoji_FREQ
GROUP BY FREQUENCY;

1 个答案:

答案 0 :(得分:0)

你可以使用PIVOT查询(https://www.techonthenet.com/oracle/pivot.php)或尝试这样的

SELECT log_message_date, 
       30 AS frequency, 
       e1_count, 
       Round(e1_count/total*100), 
       e2_count, 
       Round(e2_count/total*100), 
       e3_count, 
       Round(e3_count/total*100), 
       e4_count, 
       Round(e4_count/total*100), 
       e5_count, 
       Round(e5_count/total*100) 
FROM  ( 
                SELECT   SUM(Decode(emoji, 
                                    1, Count(1), 
                                    0)) "e1_count", 
                         SUM(Decode(emoji, 
                                    2, Count(1), 
                                    0)) "e2_count", 
                         SUM(Decode(emoji, 
                                    3, Count(1), 
                                    0)) "e3_count", 
                         SUM(Decode(emoji, 
                                    4, Count(1), 
                                    0)) "e4_count", 
                         SUM(Decode(emoji, 
                                    5, Count(1), 
                                    0)) "e5_count",
                         count(emoji) AS total, 
                         trunc(ts_create)                       AS log_message_date 
                FROM     reports 
                WHERE    ts_create > trunc(SYSDATE) - 30 
                GROUP BY trunc(ts_create))