1

我有这个简单的查询:

Select 
To_Date('2012-sep-03','yyyy-mon-dd')as Date_Of_Concern,
Count(Player_Id) as Retained
From Player
Where
(To_Date('2012-sep-03','yyyy-mon-dd')-Trunc(Init_Dtime))<=7

结果:

Date_Of_Concern         Retained
 03-Sep-12               81319

此查询计算我的数据库中在特定日期后 7 天内登录(init_dtime) 的所有玩家。

就目前而言,对于我希望了解的每个“关注日”,我将不得不多次运行此查询。有更好的解决方案吗?

4

4 回答 4

1

只需使用GROUP BY按天计算:

Select 
To_Date(Init_Dtime,'yyyy-mon-dd') as Date_Of_Concern,
Count(Player_Id) as Retained
From Player
Where
(To_Date('2012-sep-03','yyyy-mon-dd') - Trunc(Init_Dtime)) <= 7
GROUP BY To_Date(Init_Dtime,'yyyy-mon-dd')
ORDER BY To_Date(Init_Dtime,'yyyy-mon-dd')
于 2012-09-20T23:02:27.833 回答
1

如果您需要针对多个日期运行此查询,则需要一些平均值来保存多个值。我建议你使用一个NESTED TABLE对象:

CREATE TYPE my_dates AS TABLE OF DATE;
/

SELECT d.column_value AS Date_Of_Concern, count(Player_Id) AS Retained
  FROM Player
  JOIN TABLE (my_dates(to_date('2012-sep-03', 'yyyy-mon-dd'),
                       to_date('2012-sep-04', 'yyyy-mon-dd'),
                       to_date('2012-sep-05', 'yyyy-mon-dd'))) d
          ON d.column_value - trunc(Init_Dtime) BETWEEN 0 AND 7
 GROUP BY d.column_value
于 2012-09-21T10:04:06.723 回答
0

在特定日期的 7 天内

为了能够做你想做的事,你必须通过公式或日期范围知道你所说的“特定日期”。任何随机日期显然都需要用户输入该日期或修改查询以针对该日期运行(您提到的方式)。

于 2012-09-20T23:20:51.900 回答
0

不确定我是否理解正确,但这可能是您想要的。虽然可能有次优的性能。

12:32:22 HR@vm_xe> l                                                                        
  1   with player(id, dt) as (                                                              
  2     select 1, date '2012-01-01' from dual union all                                     
  3     select 2, date '2012-01-01' from dual union all                                     
  4     select 3, date '2012-01-02' from dual union all                                     
  5     select 4, date '2012-01-03' from dual union all                                     
  6     select 5, date '2012-01-04' from dual union all                                     
  7     select 6, date '2012-01-05' from dual union all                                     
  8     select 7, date '2012-01-06' from dual union all                                     
  9     select 8, date '2012-01-07' from dual union all                                     
 10     select 9, date '2012-01-08' from dual union all                                     
 11     select 10, date '2012-01-09' from dual union all                                    
 12     select 11, date '2012-01-10' from dual                                              
 13   )                                                                                     
 14     select distinct                                                                     
 15            to_char(dt, 'dd-mm-yyyy') dt                                                 
 16            ,count(*) over (order by trunc(dt) range interval '7' day preceding) week_cnt
 17       from player                                                                       
 18*     order by 1, 2                                                                      
12:32:22 HR@vm_xe> /                                                                        

DT           WEEK_CNT                                                                       
---------- ----------                                                                       
01-01-2012          2                                                                       
02-01-2012          3                                                                       
03-01-2012          4                                                                       
04-01-2012          5                                                                       
05-01-2012          6                                                                       
06-01-2012          7                                                                       
07-01-2012          8                                                                       
08-01-2012          9                                                                       
09-01-2012          8                                                                       
10-01-2012          8                                                                       

10 rows selected.                                                                           

Elapsed: 00:00:00.01                                                                        

ps不要像这样编码

(To_Date('2012-sep-03','yyyy-mon-dd')-Trunc(Init_Dtime))<=7

像这样的代码

init_time between to_date('2012-SEP-03', 'yyyy-mon-dd') and to_date('2012-SEP-03', 'yyyy-mon-dd') + 7 

除非你不关心索引,当然:)

于 2012-09-21T04:20:33.053 回答