表示这样的
时间                         设备1              设备2            设备3
2012-8-1  00:00:00            2                  3                4
2012-8-1  00:30:00            3                  4                6
2012-8-1  01:00:00            4                  5                7
2012-8-2  00:00:00            6                  6                9
2012-8-3  00:00:00            7                  9               11做了一个存储过程,两个参数 @SQL,@日期
当@日期=2012-8-1 @SQL=1时
显示日报 求2012-8-1的最大值-最小值结果
时间                         设备1              设备2            设备3                         
2012-8-1                       2                  2                3当@日期=2012-8-1 @SQL=2时
显示月报 求2012-8这个月设备的最大值-最小值
结果
时间                         设备1              设备2            设备3                         
2012-8                         5                  6                7另外
还有,如果我有两个参数@时间1,@时间2
如何判断 当@时间1=@时间2是
时间=@时间1
不等时 时间 between @时间1 and @时间2

解决方案 »

  1.   


    if @sql=1
    select CAST(时间 as date) as 时间,
    MAX(设备1)-MIN(设备1) as 设备1 ,
    MAX(设备2)-MIN(设备2) as 设备2,
    MAX(设备3)-MIN(设备3) as 设备3 
    From 表
    group by CAST(时间 as date)
    having COUNT(*)>1

    else  select CONVERT(varchar(7),时间,120) as 时间,
    MAX(设备1)-MIN(设备1) as 设备1 ,
    MAX(设备2)-MIN(设备2) as 设备2,
    MAX(设备3)-MIN(设备3) as 设备3 
    From 表
    group by CONVERT(varchar(7),时间,120)
    having COUNT(*)>1
      

  2.   

    if @SQL='2012-8-1'   then
    begain
      if @日期=1  then
        select  时间,max(设备1) - min(设备1), max(设备2) - min(设备2), max(设备3) - min(设备3)
         from tb group by 时间
      elseif @日期=2 then
        select  convert(char(7),时间120),max(设备1) - min(设备1), max(设备2) - min(设备2), max(设备3) - min(设备3)
         from tb group by convert(char(7),时间120)
            
    end
      

  3.   

    if @SQL='2012-8-1'   then
    begin
      if @日期=1  then
        select  时间,max(设备1) - min(设备1), max(设备2) - min(设备2), max(设备3) - min(设备3)
         from tb group by 时间
      elseif @日期=2 then
        select  convert(char(7),时间120),max(设备1) - min(设备1), max(设备2) - min(设备2), max(设备3) - min(设备3)
         from tb group by convert(char(7),时间120)
            
    end