原始表a001
DAte                  Indexe        Val2006-9-15 01:33:34         1         234.52578
2006-9-15 01:53:34         1         0
2006-9-15 01:33:34        2         1352.86792
2006-9-15 01:33:34         3        .875628456
2006-9-15 01:33:34        4         352.86792
2006-9-15 02:34:35         1         252.67984
2006-9-15 02:34:35         2        1287.86874
2006-9-15 02:34:35         3       .875623576
2006-9-15 02:34:35         4        287.86874
 ``````````````````````````
IndexeIndexe列的1对应t1列,4对应t2列 3对应t3列
要查找出来昨天的数据传到表a1(需要先把表a1的数据删除掉),并算出来t1列小计,想是放在作业里面运行
假如一个数据在一个小时里面有两条选数值大的哪个,假如都为0,选择前面的哪个格式如下    
d1           t1          t2    t3
01          234       352.86   0.875
02          252       287.86   0.875
`````````````````
24          '''       ''''''   '''
           小计

解决方案 »

  1.   

    IndexeIndexe列的1对应t1列,4对应t2列 3对应t3列
    这样对应不可以吗?
      

  2.   

    看看這個是不是你要的。
    --建立測試環境
    Create Table a001
    (Date DateTime,
     Indexe Int,
     Val Numeric(20,9))Create Table a1
    (d1 Char(2),
     t1 Int,
     t2 Numeric(10,2),
     t3 Numeric(10,3))
    GO
    --插入數據
    Insert a001 Select '2006-9-15 01:33:34',         1,         234.52578
    Union All Select '2006-9-15 01:53:34',         1,         0
    Union All Select '2006-9-15 01:33:34',         2,         1352.86792
    Union All Select '2006-9-15 01:33:34',         3,        .875628456
    Union All Select '2006-9-15 01:33:34',         4,         352.86792
    Union All Select '2006-9-15 02:34:35',         1,         252.67984
    Union All Select '2006-9-15 02:34:35',         2,        1287.86874
    Union All Select '2006-9-15 02:34:35',         3,       .875623576
    Union All Select '2006-9-15 02:34:35',         4,        287.86874
    --Select * From a001
    GO
    --測試
    Delete From a1Insert a1
    Select 
    Right(100+DatePart(hh,[Date]),2) As d1,
    Max(Case Indexe When 1 Then Val End) As t1,
    Max(Case Indexe When 4 Then Val End) As t2,
    Max(Case Indexe When 3 Then Val End) As t3
    From a001
    Where DateDiff(dd,[Date],GetDate())=1
    Group By Right(100+DatePart(hh,[Date]),2) Insert a1 Select '',SUM(t1),0,0 From a1Select * From a1
    --刪除測試環境
    Drop Table a001,a1
    --結果
    /*
    d1 t1 t2 t3
    01 234 352.87 .876
    02 252 287.87 .876
       486 .00 .000
    */
      

  3.   

    正解了吧Insert a1 Select '',SUM(t1),0,0 From a1
    --〉
    Insert a1 Select '小计',SUM(t1),sum(t2),sum(t3) From a1
    需要改a1表的结构
    d1 Char(2),
    --〉
    d1 Char(4),
      

  4.   

    对不起刚刚出去啦,
    Delete From biao1Insert biao1
    Select 
    Right(100+DatePart(hh,[DateAndTime]),2) As d1,
    Max(Case TagIndex When 1 Then Val End) As t1,
    Max(Case TagIndex When 4 Then Val End) As t2,
    Max(Case TagIndex When 3 Then Val End) As t3
    From Fxiaoshiliang
    Where DateDiff(dd,[DateAndTime],GetDate())=1
    Group By Right(100+DatePart(hh,[DateAndTime]),2) Insert a1 Select '',SUM(t1),0,0 From a1Select * From a1
    得到的提示是
    服务器: 消息 213,级别 16,状态 4,行 3
    插入错误: 列名或所提供值的数目与表定义不匹配。第一列是DateTime类型数据
    剩下的都是decimal类型,他自己就能处理小数的位置
    还有不是只有这3列, Indexe列是1-100多哪,我想以后能随便的添加别的列.
      

  5.   

    第一列我设置成DateTime类型数据对吗?
    剩下的都是decimal类型,他自己就能处理小数的位置
    还有不是只有这3列, Indexe列是1-100多个数字哪,一天大概有3000行原始数据吧,保存3个月是不是应该多建几个表分别放这些数据,就用一个原始表查询起来会很慢吧.想想把没有联系的原始表分开到几个表里面
    我想以后能随便的添加别的列.昨天子陌红尘老师帮我写的哪段我都理解啦