Oracle SQL--查找上周未显示的新用户

Oracle SQL--查找上周未显示的新用户,sql,oracle,Sql,Oracle,我每周使用跟踪网站用户的数据库,以XL表格的形式生成报告。我必须找到在上周生成的上一份报告中没有出现的web应用程序用户列表 数据库结构和数据(表中有两列WebAppUserName和URL): 第一周数据(以DB为单位): Tom google.com Jim google.com/test Ken google.com/test Tom

我每周使用跟踪网站用户的数据库,以XL表格的形式生成报告。我必须找到在上周生成的上一份报告中没有出现的web应用程序用户列表

数据库结构和数据(表中有两列WebAppUserNameURL):

第一周数据(以DB为单位):

  Tom                      google.com
  Jim                      google.com/test
  Ken                      google.com/test
  Tom                      google.com
  Jim                      google.com/test
 Tom                      google.com
 Jim                      google.com/test
 Ken                    google.com/test
 Ron                    google.com/test
第二周(上周)数据(以DB为单位):

  Tom                      google.com
  Jim                      google.com/test
  Ken                      google.com/test
  Tom                      google.com
  Jim                      google.com/test
 Tom                      google.com
 Jim                      google.com/test
 Ken                    google.com/test
 Ron                    google.com/test
本周(第三周)数据(以DB为单位):

  Tom                      google.com
  Jim                      google.com/test
  Ken                      google.com/test
  Tom                      google.com
  Jim                      google.com/test
 Tom                      google.com
 Jim                      google.com/test
 Ken                    google.com/test
 Ron                    google.com/test

由于Ken和Ron没有出现在上周的数据中,而他们都出现在本周的数据中,>我只需要检索Ken和Ron的信息,而不需要检索其他用户的信息。如何为此编写查询。

表中必须有日期列,以标识用户何时访问URL

您可以使用如下类似的
不存在

Select t.* from
Your_table t
Where trunc(t.date,'IW') = trunc(SYSDATE,'IW') -- fetch current week data
AND NOT EXISTS 
(SELECT 1 
FROM YOUR_TABLE TT
WHERE trunc(t.date,'IW') - trunc(TT.date,'IW') = 7 -- exclude user who appeared last week
AND TT.WEBAPPUSERNAME = T.WEBAPPUSERNAME) -- you can also add condition for URL in not exists if required in your logic
干杯

行编号()
是一种典型的解决方案:

select t.*
from (select t.*,
             row_number() over (partition by WebAppUserName order by datecol) as seqnum
      from t
     ) t
where seqnum = 1;
通常,相关子查询具有良好的性能:

select t.*
from t
where t.datecol = (select min(t2.datecol)
                   from t t2
                   where t2.WebAppUserName = t.WebAppUserName
                  );

欢迎来到SO。目前我们无法回答您的问题。这里有一些关于这个数据库结构的信息和示例数据:WebAppUserName URL Tom google.com我当然希望这不是你的数据库结构。至少没有日期。很抱歉没有添加日期列。但它在数据库中