我正在寻找如何根据用户和日期编写计数查询。例如,如果我有以下数据:
Agent | Date_1
Joe Bloggs | 06-jun-16 10:35:00
Joe Bloggs | 06-jun-16 10:36:00
Joe Bloggs | 06-jun-16 10:46:00
Joe Bloggs | 07-jun-16 09:36:00
我能够写下以下内容:
我目前的查询如下:
SELECT
AGENT
,DATE_1
,ROW_NUMBER() OVER (PARTITION BY AGENT ORDER BY DATE_1) AS COUNT_1
FROM
TABLE_1
;
这会根据我的数据为每一行提供一个顺序计数。但是我希望06-jun-16的数量只有1,因为用户和日期一样,但我不知道怎么能得到它。我想要的是:
Agent Name | Date_1 | Count_1
Joe Bloggs | 06-jun-16 | 1
Joe Bloggs | 07-jun-16 | 1
以下是我的表的创建表和插入语句:
CREATE TABLE TABLE_1
(
"AGENT" VARCHAR2(12 BYTE),
"DATE_1" DATE
);
插入声明
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 09:47:23','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:07:47','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:16:48','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:32:31','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:33:35','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 10:49:20','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 11:14:17','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 14:08:13','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
Insert into TABLE_1 (AGENT,DATE_1) values ('Joe Bloggs',to_date('07-JUN-16 15:31:05','DD-MON-RR HH24:MI:SS'));
根据以上数据,我希望将所有大于30分钟的触摸统计为单个计数,任何介于30分钟之间的事物都不会增加计数。
根据我的数据,第一次触摸是09:47:23所以从1开始,第二次独特的触摸是10:07:47(基于下一个日期期间),大约20分钟后因此计数保持在1,现在看第三个不同的时间,这是在10:16:48,比之前的9分钟,所以计数仍然保持为1.
所以实际上它是基于前一个时间戳的滚动30分钟计数,如果其低于30分钟的计数与之前的时间相同,如果其更大的计数增加1 ...
希望这更有意义。
答案 0 :(得分:-1)
这将为您提供您指定的结果,每个代理和日期组合的三十分钟窗口数。
select agent
, trunc(date_1) as date_1
, sum(intvl)+1 as count_1
from (
SELECT
AGENT
, DATE_1
, case when date_1 >= lag(date_1) over (partition by agent order by date_1)
+ to_dsinterval('0 0:30:0')
then 1 else 0 end as intvl
FROM
TABLE_1
)
group by agent
, trunc(date_1)
order by 1, 2
/
LAG()分析函数允许我们将DATE_1的当前值与前一个值进行比较,后者使用30分钟的INTERVAL()进行扩充,以测试所需的窗口。截断从DATE_1列中删除时间元素,每天给出一个结果。
答案 1 :(得分:-1)
简单。只需编写如下的外部选择语句:
select a.*
from
(SELECT
AGENT
,DATE_1
,ROW_NUMBER() OVER (PARTITION BY AGENT ORDER BY DATE_1) AS COUNT_1
FROM
PROJECT ) a
where a.COUNT_1 = 1 ;