表table
字段
UniqueId  CardID  Time       Date      Flag
1         901      2:00  2009/01/01    true
2         901      3:00  2009/01/01    false
3         902      3:00  2009/01/01    false
4         902      4:00  2009/01/01    true我想统计  对于某cardID 在2009/01/01这天 time最大的那条记录的flag是true 符合这样条件的所有记录的个数

解决方案 »

  1.   

    --建立测试环境
    set nocount on
    create table test(UniqueId varchar(20),CardID varchar(20),Time varchar(20),Date varchar(20),Flag varchar(20))
    insert into test select '1','901','2:00','2009/01/01','true'
    insert into test select '2','901','3:00','2009/01/01','false'
    insert into test select '3','902','3:00','2009/01/01','false'
    insert into test select '4','902','4:00','2009/01/01','true'
    go
    --测试
    select * from test a where date='2009/01/01' and flag='true' and not exists(
    select 1 from test where cardid=a.cardid and date=a.date and time>a.time)--删除测试环境
    drop table test
     set nocount off
      

  2.   

    cardID 在2009/01/01这天 time最大的那条记录的flag是true 符合这样条件的所有记录的个数
    不是只有一条?.
      

  3.   

    select count(1) as 个数 from tab
    where flag='true' and date='2009/01/01'
      

  4.   

     create table test(UniqueId varchar(20),CardID varchar(20),Time varchar(20),Date varchar(20),Flag varchar(20))
    insert into test select '1','901','2:00','2009/01/01','true'
    insert into test select '2','901','3:00','2009/01/01','false'
    insert into test select '3','902','3:00','2009/01/01','false'
    insert into test select '4','902','4:00','2009/01/01','true'
     select count(1) as 个数  from test  a where not exists (select 1 from test where CardID=a.CardID and Date=a.Date and Time>a.Time) and Flag='true' and Date='2009/01/01'
    /*
    个数
    --
    1
    */
     drop table test
      

  5.   


    对于以上数据来说是只有一条
    key是第一个字段 忘记说明了不好意思
      

  6.   


    time最大  这个条件你没写