现在有张表如下:                          希望的结果是:
 id     记帐  类型    金额              id     记帐  类型   金额
 1       1      0     xxx               5      记1   付     xxx
 2       1      0     xxx               6      记1   收     xxx
 3       1     -1     xxx               7      记2   付     xxx
 4       1     -1     xxx               8      记2   收     xxx
 5       1     -1     xxx               9      记2   收     xxx
 6       2      0     xxx               10     记3   付     xxx
 7       1     -1     xxx               11     记3   收     xxx
 8       2      0     xxx               12     记4   付     xxx
 9       3      0     xxx               13     记4   收     xxx
 10      1     -1     xxx               14     记4   收     xxx
 11      2      0     xxx               15     记4   收     xxx
 12      1     -1     xxx               
 13      2      0     xxx               
 14      3      0     xxx               
 15      4      0     xxx              现在需要在datagrid中显示时 根据"记帐"字段的内容比如连续都是"1"则只显示最后一个,应此直到第五条记录开始显示,而且根据内容(比如连续的1,2,3表示是一组,即在"记帐"里遇到的第一组用记1,第二组用记2表示,依次类推) 
 这该怎么做呀?头都晕了,请给些指点,谢了!

解决方案 »

  1.   

    还要判断的话,用MSFlexGrid就可以轻松做好.
      

  2.   

    --建表、添加测试数据:
    create table T(id int,n int)
    insert T select 1,       1 
       union select 2,       1
       union select 3,       1
       union select 4,       1
       union select 5,       1
       union select 6,       2 
       union select 7,       1 
       union select 8,       2 
       union select 9,       3 
       union select 10,      1 
       union select 11,      2
       union select 12,      1             
       union select 13,      2             
       union select 14,      3             
       union select 15,      4--问题解答:
    declare @m int, @n intcreate table #t(id int, n int, m int)
    insert #t 
    select a.*,1
    from T a join (select id-1 as id,n from T ) b
    on a.id=b.id and a.n<>b.n
    union select *,1 from (select top 1 id,n from T order by id desc) c set @m=0
    set @n=1update #t set @m=case when @n=n-1 then @m else @m+1 end,
                  @n=n,
                  m=@m
      select * from #t--删除测试用表:
    drop table T,#t--测试结果:
    id          n           m           
    ----------- ----------- ----------- 
    5           1           1
    6           2           1
    7           1           2
    8           2           2
    9           3           2
    10          1           3
    11          2           3
    12          1           4
    13          2           4
    14          3           4
    15          4           4(所影响的行数为 11 行)
      

  3.   

    to: tztz520(午夜逛街) 
    能具体说说吗? 初次做这个东西,还请高手指点!