如果我有以下数据
A | 01/01/2008 00:00:00
B | 01/01/2008 01:00:00
A | 01/01/2008 11:59:00
C | 02/01/2008 00:00:00
D | 02/01/2008 01:00:00
D | 02/01/2008 20:00:00
我想只选择标识符(A,B,C或D)在12小时内发生两次的记录。在上面的这个例子中,这只是'A'
任何人都可以帮忙(这是针对Oracle数据库的)
由于
中号
答案 0 :(得分:6)
Select Distinct A.Identifer
From Table A
Join Table B -- EDIT to eliminate self Joins (to same row)
On A.PrimKey <> B.PrimaryKey
And A.Identifer = B.Identifer
-- EDIT to fix case where 2 at same time
And A.OccurTime >= B.OccurTime
And A.OccurTime < B.OccurTime + .5
并实施评论中提出的问题, (忽略不同日期的记录)
- 对于SQL Server,
Select Distinct A.Identifer
From Table A
Join Table B
On A.PrimKey <> B.PrimaryKey
And A.Identifer = B.Identifer
-- EDIT to fix case where 2 at same time
And A.OccurTime >= B.OccurTime
And A.OccurTime < B.OccurTime + .5
Where DateDiff(day, A.OccurTime, B.OccurTime) = 0
- 或oracle ...
Select Distinct A.Identifer
From Table A
Join Table B
On A.PrimKey <> B.PrimaryKey
And A.Identifer = B.Identifer
-- EDIT to fix case where 2 at same time
And A.OccurTime >= B.OccurTime
And A.OccurTime < B.OccurTime + .5
Where Trunc(A.OccurTime) = Trunc(B.OccurTime)
答案 1 :(得分:2)
Select
A.Id
From
YourTable A
Where
A.YourDateTime Between :StartDateTime and :EndDateTime
Group By
A.Id
Having
COUNT(A.Id) = 2
答案 2 :(得分:2)
我没有检查威廉的查询,但我会认真考虑使用他所拥有的东西。分析是炸弹。每当你发现自己加入一个表回到自身时,几乎可以肯定是一个使用分析的机会,并且会在每次引用两次的表中执行查询。
您将惊讶分析解决方案的速度会快多少。
答案 3 :(得分:1)
SELECT identifier
FROM table_name outer
WHERE EXISTS( SELECT 1
FROM table_name inner
WHERE inner.identifier = outer.identifier
AND inner.date_column BETWEEN outer.date_column AND outer.date_column + interval '12' hour
AND inner.rowid != outer.rowid )
答案 4 :(得分:1)
我不是100%确定您的要求,但这可能会为您提供有关如何做您需要的一些想法。例如,你说的确是2;如果有3次出现怎么办?等
create table t (ident varchar2(16), occurance timestamp);
insert into t (ident, occurance) values ('a', to_date('20080101000000', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('b', to_date('20080101010000', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('a', to_date('20080101115900', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('c', to_date('20080102000000', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('d', to_date('20080102010000', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('d', to_date('20080102200000', 'yyyymmddhh24miss'));
insert into t (ident, occurance) values ('d', to_date('20080103020000', 'yyyymmddhh24miss'));
select ident, occurance
from
(
select ident, occurance,
lag(occurance) over (partition by ident order by occurance) previous,
lead(occurance) over (partition by ident order by occurance) next
from t
)
where
((occurance-previous<interval'12:00' hour to minute and extract(day from occurance) = extract(day from previous))
or (next-occurance<interval'12:00' hour to minute and extract(day from occurance) = extract(day from next)))
/
答案 5 :(得分:0)
SELECT namecol FROM tbl A
WHERE EXISTS (
SELECT 1 from tbl B
WHERE b.namecol = a.namecol
AND b.timestamp > a.timestamp
AND b.timestamp - 0.5 <= a.timestamp )