Oracle SQL - 使用计数查询编写连接

时间:2014-09-09 13:04:04

标签: mysql sql oracle

我目前正在记录去年登录过的数据库中的用户数。

这是我正在使用的示例架构:http://sqlfiddle.com/#!4/7dfb6/15(复制如下)

我有这两个问题:

查询1:

select distinct userid from tosuser tu;

查询2:

select count (*) from tosevent te where te.LASTUPDATEDATE BETWEEN 
to_date('01/01/2014', 'dd/mm/yyyy') AND to_date('31/12/2014', 'dd/mm/yyyy');

我希望加入,得到这样的结果:

USERNAME    COUNT
FRED         7
MARY         2

其中count表示用户在去年登录的次数。 (我目前将这一年作为java参数传递)

我最接近的是:

select distinct username, LASTUPDATEDATE from tosuser tu, tosevent te where 
te.LASTUPDATEDATE BETWEEN 
to_date('01/01/2014', 'dd/mm/yyyy') AND to_date('31/12/2014', 'dd/mm/yyyy')
and te.username = tu.userid and tu.userenabled='1' and tu.userid!='OASIS';

这给了我每次登录活动。 只是想知道你是否知道我将如何使用Oracle语法编写此查询?


如果消失的话,

来自SQL小提琴的模式:

CREATE TABLE tosuser ( userid     VARCHAR2(30) PRIMARY KEY, IsDeleted    INT NOT NULL, UserEnabled    INT,ExternalUser  INT);

CREATE TABLE tosevent ( id      VARCHAR2(30) PRIMARY KEY, username VARCHAR2(30), DATA3 VARCHAR2(30), LASTUPDATEDATE DATE  );

INSERT into tosuser (userid, IsDeleted, UserEnabled, ExternalUser) values ('OASIS',1,1,1);    
INSERT into tosuser (userid, IsDeleted, UserEnabled, ExternalUser) values ('FRED',1,1,1);
INSERT into tosuser (userid, IsDeleted, UserEnabled, ExternalUser) values ('MARY',1,1,1);
INSERT into tosuser (userid, IsDeleted, UserEnabled, ExternalUser) values ('TOM',1,1,1);
INSERT into tosuser (userid, IsDeleted, UserEnabled, ExternalUser) values ('MICHEAL',1,1,1);

INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (1,'FRED','successful logon',to_date('08/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (2,'FRED','successful logon',to_date('09/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (3,'FRED','successful logon',to_date('10/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (4,'FRED','successful logon',to_date('11/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (5,'FRED','successful logon',to_date('12/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (6,'MARY','successful logon',to_date('08/09/2014', 'dd/mm/yyyy'));
INSERT into tosevent (id, username, DATA3, LASTUPDATEDATE) values (7,'MARY','successful logon',to_date('09/09/2014', 'dd/mm/yyyy'));

感谢。

2 个答案:

答案 0 :(得分:3)

我想你只想要group by

select username, count(*)
from tosuser tu join
     tosevent te 
     on te.username = tu.userid
where te.LASTUPDATEDATE BETWEEN to_date('01/01/2014', 'dd/mm/yyyy') AND
                                to_date('31/12/2014', 'dd/mm/yyyy') and
      tu.userenabled = '1' and tu.userid <> 'OASIS'
group by username;

你需要小心使用日期。如果LastUpdateDate有时间组件,那么您希望将其标记为:

where te.LASTUPDATEDATE >= date '2014-01-01' and te.LASTUPDATEDATE < date '2015-01-01'

你也可以通过查看年份来写这个:

where extract(year from LASTUPDATEDATE) = 2014

但是,此版本可能无法像以前的版本那样有效地使用索引。

答案 1 :(得分:1)

您无法以这种方式使用DISTINCT,因为它适用于SELECT中的所有字段,而不仅仅适用于以下字段。 您应该使用GROUP BY

select username, LASTUPDATEDATE
from tosuser tu, tosevent te 
where 
te.LASTUPDATEDATE BETWEEN to_date('01/01/2014', 'dd/mm/yyyy') AND to_date('31/12/2014', 'dd/mm/yyyy')
and te.username = tu.userid 
and tu.userenabled='1' 
and tu.userid!='OASIS'
GROUP BY username

作为一个有点相关的评论,如果您看到类似的内容:

select distinct(foo), bar ...

然后你应该检查那个查询,因为它可能没有做它想要做的事情。 SELECT

相同
select distinct foo, bar ...