数据结构
股票代码        股票名称      是否持仓  操作时间
20300348 长亮科技 0 2012-09-05 09:51:55.290
20300328 宜安科技 1 2012-09-03 13:20:30.440
20300328 宜安科技 0 2012-08-31 10:41:54.910
20300348 长亮科技 1 2012-08-31 10:37:58.310
20300348 长亮科技 0 2012-08-28 09:55:14.793 上面 是否持仓字段 0 代表还在持仓  1 清仓 我想找到 所有持仓的股票的最新的一条交易时间,也就是说 是否持仓字段 全部是0的股票的最新交易时间

解决方案 »

  1.   

    select 股票代码, max(操作时间)
    from t
    where 是否持仓 = 1
    group by 股票代码
      

  2.   

    哦,应该 是否持仓 = 0select 股票代码, max(操作时间)
    from t
    where 是否持仓 = 0
    group by 股票代码
      

  3.   


    -->try
    select 股票代码,操作时间=(select max(操作时间) from 表 where t.股票代码=股票代码) from 表 t
    group by 股票代码
      

  4.   

    select 股票代码,操作时间=(select max(操作时间) from 表 where 是否持仓=0 and t.股票代码=股票代码) from 表 t
    group by 股票代码
      

  5.   

    select 股票代码, max(操作时间)
    from t
    where 是否持仓 = 0
    group by 股票代码
      

  6.   

    搞糊涂了,写得有点多此一举,
    下面这样就可以达到相同的效果
    select 股票代码, max(操作时间) 操作时间 from t
    where 是否持仓 = 0 
    group by 股票代码
      

  7.   

    select a.股票代码,a.操作时间  from (select 股票代码, max(操作时间)as 操作时间
    from t  
    where 是否持仓 = 0 
    group by 股票代码) as a 
    left join 
    (select 股票代码, max(操作时间)as 操作时间
    from t
    where 是否持仓 = 1
    group by 股票代码 ) as b   on  a.股票代码=b.股票代码 where a.操作时间
    >b.操作时间
      

  8.   


    select 股票代码,股票名称, max(操作时间) as 操作时间 from test
    where 是否持仓=0
    group by 股票代码,股票名称
      

  9.   

    所有股票为0和where条件中是否持仓为0不一样么。不一定都是持仓的吗?那你怎么区分股票为0的中哪些是 是否持仓为0的数据
    --以你给的表数据,就是下面这么写的
    select 股票代码, max(操作时间) 操作时间 from t
    where 是否持仓 = 0  
    group by 股票代码
      

  10.   


    declare @test table(股票代码 int, 股票名称 nvarchar(4),是否持仓 int, 操作时间 datetime)
    insert into @test
    select 20300348, N'长亮科技', 0, '2012-09-05 09:51:55.290' union all
    select 20300328, N'宜安科技', 1, '2012-09-03 13:20:30.440' union all
    select 20300328, N'宜安科技', 0, '2012-08-31 10:41:54.910' union all
    select 20300348, N'长亮科技', 1, '2012-08-31 10:37:58.310' union all
    select 20300348, N'长亮科技', 0, '2012-08-28 09:55:14.793'
    ;with cte as
    (
    select row_number() over(partition by 股票代码 order by 操作时间 desc) rn,* from @test
    )
    select 股票代码, 股票名称,是否持仓, 操作时间 from cte where rn=1 and 是否持仓=0
    /*
    股票代码        股票名称 是否持仓        操作时间
    ----------- ---- ----------- -----------------------
    20300348    长亮科技 0           2012-09-05 09:51:55.290*/
      

  11.   


    还是有个问题啊declare @test table(股票代码 int, 股票名称 nvarchar(4),是否持仓 int, 操作时间 datetime)
    insert into @test
    select 20300348, N'长亮科技', 0, '2012-09-05 09:51:55.290' union all
    select 20300328, N'宜安科技', 0, '2012-09-03 13:20:30.440' union all
    select 20300328, N'宜安科技', 0, '2012-08-31 10:41:54.910' union all
    select 20300328, N'宜安科技', 1, '2012-09-03 13:20:30.440' union all
    select 20300328, N'宜安科技', 0, '2012-08-31 10:41:54.910' union all
    select 20300348, N'长亮科技', 1, '2012-08-31 10:37:58.310' union all
    select 20300348, N'长亮科技', 0, '2012-08-28 09:55:14.793'
    ;with cte as
    (
        select row_number() over(partition by 股票代码 order by 操作时间 desc) rn,* from @test
    )
    select 股票代码, 股票名称,是否持仓, 操作时间 from cte where rn=1 and 是否持仓=0您执行这个试试  始终都是那一条数据呢??