select * from [臨時表] where [操作時間] in (
  select max([操作時間]) from [臨時表] group by
  datepart(yyyy, [操作時間]),
  datepart(mm, [操作時間]),
  datepart(d, [操作時間])
  )

解决方案 »

  1.   

    你可以用trigger 来自动实现这一目标!
    for each row 
    after insert or update 
    please replace ur 结存 with 结存(+/-)in/out number,
    你只需要MAX(DATE), 结存既可!
     
      

  2.   

    select to_char(date,'yyyymmdd') as datestring,jc having greatest(date)
    group by to_char(dte,'yyyymmdd') 
      

  3.   

    select to_char(date,'yyyymmdd') as datestring,jc from my_table having greatest(date)
    group by to_char(dte,'yyyymmdd') 
      

  4.   

    谢谢各位,不过我要实现的功能不是求得某一天的最后一次的结存,而是要一次性的把一段时间内的每一天的包括进、出库的数量、金额以及结存的数量、金额同时输出,上述各Select好像都无法实现。Trigger我没有试,因为我不太会用 :-)
      

  5.   

    其实关键是要找到每天最后的一笔数据,tirgger 不是个好东东,在业务繁忙时就会降低性能。
    在 table 中增加一个  字段,用来记录。
    不然在用 sql 不能汇中
      

  6.   

    你的问题我想是这样;
      结存数因为有历史性,所以你要用STORE PROCEDURE 
      SELECT 能完成你的要求!
      下面的过程就是你的要求结果!!!????
       这只是oracle 下的解法:
      下面的过SQL 中用了DECODE 和ASSIGN ,请你查一下assign 的用法,我手边无资料
      可能连ASSIGN 都不是。这个函数是判断一个数值的正负性的。返回值的表示也请查一
      下;decode(assign(a.last_time-b.do_date),+,1,0,1,0) 这一段你可以根据两函
      数的用法重写。 
    1。 我认为你的表结构如下:
      main_table :
       {do_date date ; //实时
       store_id number(n,0) ;
       inout  number(n,0) // in means + ,out means -?
       cost number(n,m),
    }2 。请CREATE A VIEW AS BELOW :
     
        create view last_io_per_date as
        select to_char(do_date,'yyyymmdd') as datestring,
               store_id,
               max(do_date) as last_time 
           from  main_table
           group by  to_char(do_date,'yyyymmdd'),store_id ;3. 你想要的:  select a.datestring,a.stor_id,
        sum(b.inout*decode(assign(a.last_time-b.do_date),+,1,0,1,0)) 
         as jcnum
        from last_io_per_date a ,main_table b
        where a.store_id=b.store_id 
         // and a.datestring between begin_day and end_day
        group by a.datestring,a.store_id ;
    the query is your wanted ?!!! I think so!!!
    注意::
    decode(assign(a.last_time-b.do_date),+,1,0,1,0) 这一段请你查一查书;
                descible  ::  +,1  表示 a.last_time-b.do_date> 0, value=1
                                0,1  表示 a.last_time-b.do_date=0, value =1
                                0    表示 a.last_time-b.do_date<0, value = 0 
                                            
      

  7.   

    store_id means diffrent code for ur material
      

  8.   

    本期结存=期初结存+本期入库-本期出库+本期盘存盈亏
    从你的描述看,本期盘存盈亏为0,上期结存为0我给你一个算法(只做数量,金额同):select 日期,品种,sum(数量) as 数量
          into 日库存变化临时表
          from (select 日期,品种,-数量 as 数量
                     from 销售表
                union
                select 日期,品种,数量
                     from 入库表
               ) temp
          group by 日期,品种select a.日期,a.品种,sum(b.数量) as 当日结存数量 
             /*如果有期初结存:sum(b.数量)+期初结存 as 当日结存数量 */
          from 日库存变化临时表 a,
               日库存变化临时表 b
          where a.日期>=b.日期 and
                a.品种=b.品种
          group by a.日期,a.品种
      

  9.   

    decode(assign(a.last_time-b.do_date),+,1,0,1,0) change last sentences as belowdecode(sign(a.last_time-b.do_date),1,1,0,1,0)