售水记录表 表一:
序号1 姓名 时间 售水量 金额
1 张 2001-1-1 11 10
2 王 2001-7-5 22 20
3 李 2002-5-5 33 30
4 赵 2003-6-6 44 40
5 王 2002-5-6 33 30
6 赵 2004-1-1 20 15
7 王 2006-6-6 3 2
需要结果:
姓‘王’的全部售水记录:
序号1 姓名 时间 售水量 金额
1 王 2001-7-5 22 20
2 王 2002-5-6 33 30
3 王 2006-6-6 3 2某用户某N次(N=3)次售水情况:
姓名 第一次售水时间 第一次售水量 第二次售水时间 第二次售水量 第三次售水时间 第三次售水量
张 2001-1-1 11 0 0
王 2001-7-5 22 2002-5-6 33 2006-6-6 3
李 2002-5-5 33 0 0
赵 2003-6-6 44 2004-1-1 20 0

解决方案 »

  1.   

    第一个结果:
    select * from Tab where 姓名=王
      

  2.   

    第一个结果:
    select * from Tab where 姓名=王 order by 序号1
      

  3.   

    第一个结果:
    select identity(int,1,1) as 序号1, 姓名, 时间, 售水量, 金额
     into #tmp from 表一
    select * from #tmp
      

  4.   

    --第一问:
    select identity(int,1,1) 序号1, 姓名, 时间, 售水量, 金额
    into #t1
    from 表一 
    where 姓名='王'
    order by 时间
    select * from #t1--第二问:
    select identity(int,1,1) 序号1, 姓名, 时间, 售水量, 金额
    into #t2
    from 表一 
    order by 姓名,时间select  姓名
    ,时间1=(select 时间 from #t2 where 姓名=a.姓名 and 序号1=a.序号1)
    ,水量1=(select 水量 from #t2 where 姓名=a.姓名 and 序号1=a.序号1)
    ,时间2=(select 时间 from #t2 where 姓名=a.姓名 and 序号1=a.序号1+1)
    ,水量2=(select 水量 from #t2 where 姓名=a.姓名 and 序号1=a.序号1+1)
    ,时间3=(select 时间 from #t2 where 姓名=a.姓名 and 序号1=a.序号1+2)
    ,水量3=(select 水量 from #t2 where 姓名=a.姓名 and 序号1=a.序号1+2)
    from (select min(序号1) 序号1,姓名 from #t2 group by 姓名) a
      

  5.   

    你在后面加上order by 字段 ASC
    或者是:order by 字段 EESC ASC(升序)
     DESC(降序)
      

  6.   

    1:你什么数据库?2:我的在SQl server数据库
    create table 表一(序号1 int identity(1,1), 姓名 varchar(10), 时间 datetime, 售水量 int, 金额 int)insert 表一( 姓名, 时间, 售水量, 金额)
    select '王','2004-10-01',23,34
    union select '张','2004-10-01',23,34
    union select '王','2004-10-02',223,334
    union select '张','2004-10-02',2343,344
    union select '王','2004-10-03',243,34
    union select '王','2004-10-03',243,344-----------
    --第一问:
    drop table #t1
    select identity(int,1,1) 序号1, 姓名, 时间, 售水量, 金额
    into #t1
    from 表一 
    where 姓名='王'
    order by 时间select * from #t1
    -----------------------
    序号1      姓名         时间                       售水量         金额          
    1           王          2004-10-01 00:00:00.000    23          34
    2           王          2004-10-02 00:00:00.000    223         334
    3           王          2004-10-03 00:00:00.000    243         34
    4           王          2004-10-03 00:00:00.000    243         344(所影响的行数为 4 行)
      

  7.   

    --第二问:
    declare @n int
    declare @sql varchar(8000)
    set @sql='select  姓名 '
    set @n=3 --设置次数为3drop table t2
    select identity(int,1,1) 序号1, 姓名, 时间, 售水量, 金额
    into t2
    from 表一 
    order by 姓名,时间
    declare @i int
    set @i=0
    while @n>@i
    begin
     set @i=@i+1
     set @sql= @sql+',时间'+convert(varchar(2),@i)+'=(select 时间 from t2 where 姓名=a.姓名 and 序号1=a.序号1+'+convert(varchar(2),@i-1)+')'
     set @sql= @sql+',水量'+convert(varchar(2),@i)+'=(select 售水量 from t2 where 姓名=a.姓名 and 序号1=a.序号1+'+convert(varchar(2),@i-1)+')'
    end
    set @sql= @sql+' from (select min(序号1) 序号1,姓名 from t2 group by 姓名) a 'exec( @sql)
    ----------
    姓名         时间1      水量1         时间2   水量2         时间3 水量3         
    王          2004-10-01 23          2004-10-02 223         2004-10-03 243
    张          2004-10-01 23          2004-10-02 2343        NULL  NULL
      

  8.   

    谢谢 xhh_88(三友)  师父!!