如何将24小时记录与另一张表进行比较

时间:2016-09-13 08:43:43

标签: sql sql-server

TableA
MemberID    property    LoginTime
1           A           2016-09-12 1300        
1           A           2016-09-12 1400        
1           B           2016-09-13 1300        

TableB
MemberID    profit    property        LoginTime
1           100       A               2016-09-14 1400
1           -80       B               2016-09-14 1200
1           200       A               2016-09-14 1300
1           300       B               2016-09-14 1400
3           800       A               2014-09-14 1400
Expected Result
ResultTable
MemberID    profit      property        LoginTime
1           200         A               2016-09-14 1pm
1           300         B               2016-09-14 2pm
3           800         A               2014-09-14 2pm

如何编写查询以满足以下条件:

  • TableB中的成员记录不在TableA
  • 在TableB中的特定属性中的成员记录,与TableA相比,logintime大于24小时对同一属性
  • TableB充当主表。

5 个答案:

答案 0 :(得分:1)

试试这个。

select * 
from tableB b
where not exists (
      select 1
      from tableA a
      where a.MemberID = b.MemberID and a.Property = b.Property
          and b.LoginTime between a.LoginTime and dateadd(hh,24, a.LoginTime) )

答案 1 :(得分:0)

这与问题中的结果集完全相同:

declare @TableA table (MemberID int, property char(1), LoginTime datetime)
declare @TableB table (MemberID int, profit int, property char(1), LoginTime datetime)

insert into @TableA values (1, 'A', '20160913 12:00')
insert into @TableA values (1, 'B', '20160913 13:00')

insert into @TableB values (1, 100, 'A', '20160914 11:00')
insert into @TableB values (1, -80, 'B', '20160914 12:00')
insert into @TableB values (1, 200, 'A', '20160914 13:00')
insert into @TableB values (1, 300, 'B', '20160914 14:00')
insert into @TableB values (3, 800, 'A', '20160914 14:00')

select B.*
from   @TableB B
  left outer join @TableA A on B.MemberID = A.MemberID
                           and B.Property = A.Property
where DATEDIFF(hh, isnull(A.LoginTime, '19000101'), B.LoginTime) >= 24

编辑:操作系统提供了新的示例数据,因此我不得不调整查询:

declare @TableA table (MemberID int, property char(1), LoginTime datetime)
declare @TableB table (MemberID int, profit int, property char(1), LoginTime datetime)

insert into @TableA values (1, 'A', '20160912 13:00')
insert into @TableA values (1, 'A', '20160912 14:00')
insert into @TableA values (1, 'B', '20160913 13:00')

insert into @TableB values (1, 100, 'A', '20160914 14:00')
insert into @TableB values (1, -80, 'B', '20160914 12:00')
insert into @TableB values (1, 200, 'A', '20160914 13:00')
insert into @TableB values (1, 300, 'B', '20160914 14:00')
insert into @TableB values (3, 800, 'A', '20160914 14:00')

select B.MemberID, B.profit, B.property, B.LoginTime
from   @TableB B
  left outer join @TableA A on B.MemberID = A.MemberID
                           and B.Property = A.Property
where DATEDIFF(hh, isnull(A.LoginTime, '19000101'), B.LoginTime) > 24
group by B.MemberID, B.profit, B.property, B.LoginTime

据我了解他的标准,所需的输出应该是这样的:

预期结果 ResultTable

MemberID    profit      property        LoginTime
1           100         A               2016-09-14 2pm
1           200         A               2016-09-14 1pm
1           300         B               2016-09-14 2pm
3           800         A               2014-09-14 2pm

答案 2 :(得分:0)

select
    B.*
from
    TableB B
left join
    TableA A        on (B.MemberID = A.MemberID and B.Property = A.Property)
left join
    TableA A1       on (B.MemberID = A1.MemberID and B.Property = A1.Property and B.logintime > dateadd(hh, 24, A1.loginTime))
where
    A.MemberID is null or A1.MemberID is not null

答案 3 :(得分:0)

查询采用MSSQL格式。

select  b.* from @TableB B 
left join @TableA A 
on b.MemberID=a.MemberID and b.property=a.property
where datediff(hour,a.logintime,b.logintime)>=24 
or a.MemberID is null

答案 4 :(得分:0)

给它一个旋转,应该很简单,使用LEFT JOIN,然后确保连接tabled中的字段为NULL。 在本地测试中,我得到了问题中指定的相同结果集 选择 * 来自@tableB b LEFT JOIN @tableA作为a.MemberId = b.MemberId     和a.Property = b.Property     和a.LoginTime和dateadd之间的b.LoginTime(hh,24,a.LoginTime) 其中a.MemberId为NULL