名称         日期             时间                  通道一          通道二
1 2006-03-06 00:01:08 0.42       0.20
2 2006-03-06 00:02:11          0.43        0.10
         3   2006-03-06 00:03:14 0.44        0.10
         1 2006-03-06 00:04:17 0.44      0.20
2 2006-03-06 00:05:21 0.41      0.20
3 2006-03-06 00:06:24 0.41      0.10
1 2006-03-06 00:07:27 0.44      0.00
2 2006-03-06 00:08:30 0.42      0.10
3 2006-03-06 00:09:33 0.44      0.00
1 2006-03-06 00:10:36 0.40      0.20 
2 2006-03-06 00:11:40          0.44      0.20
3 2006-03-06 00:12:43 0.44      0.30
1 2006-03-06 00:13:46 0.42      0.10
2 2006-03-06 00:14:49 0.42      0.20
3 2006-03-06 00:15:52 0.45      0.30
1 2006-03-06 00:16:55 0.42      0.10 
2 2006-03-06 00:17:59 0.41      0.30
         3 2006-03-06 00:19:02 0.42      0.50 
我想找到  分机号1到3 通道一和通道二的最大值和最小值,以及出现最大值和最小值的时间(若最大值出现N次,选择第一次出现的时间)
想要的结果
名称     通道一Max   时间      通道一Min   时间        通道二Max    时间         通道二min    时间
1 0.44   00:04:17       0.40     00:10:36       0.20     00:01:08        0.00      00:07:27
2 0.44      00:11:40       0.41     00:05:21       0.30     00:17:59        0.10      00:08:30
3        0.45      00:15:52       0.41     00:06:24       0.50     00:19:02        0.00      00:09:33

解决方案 »

  1.   

    --行列互转
    /******************************************************************************************************************************************************
    以学生成绩为例子,比较形象易懂整理人:中国风(Roy)日期:2008.06.06
    ******************************************************************************************************************************************************/--1、行互列
    --> --> (Roy)生成測試數據
     
    if not object_id('Class') is null
    drop table Class
    Go
    Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
    Insert Class
    select N'张三',N'语文',78 union all
    select N'张三',N'数学',87 union all
    select N'张三',N'英语',82 union all
    select N'张三',N'物理',90 union all
    select N'李四',N'语文',65 union all
    select N'李四',N'数学',77 union all
    select N'李四',N'英语',65 union all
    select N'李四',N'物理',85 
    Go
    --2000方法:
    动态:declare @s nvarchar(4000)
    set @s=''
    Select  @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
    from Class group by[Course]
    exec('select [Student]'+@s+' from Class group by [Student]')
    生成静态:select 
    [Student],
    [数学]=max(case when [Course]='数学' then [Score] else 0 end),
    [物理]=max(case when [Course]='物理' then [Score] else 0 end),
    [英语]=max(case when [Course]='英语' then [Score] else 0 end),
    [语文]=max(case when [Course]='语文' then [Score] else 0 end) 
    from 
    Class 
    group by [Student]GO
    动态:declare @s nvarchar(4000)
    Select  @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
    exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')生成静态:
    select * 
    from 
    Class 
    pivot 
    (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
    /*
    Student 数学          物理          英语          语文
    ------- ----------- ----------- ----------- -----------
    李四      77          85          65          65
    张三      87          90          82          78(2 行受影响)
    */------------------------------------------------------------------------------------------
    go
    --加上总成绩(学科平均分)--2000方法:
    动态:declare @s nvarchar(4000)
    set @s=''
    Select  @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
    from Class group by[Course]
    exec('select [Student]'+@s+',[总成绩]=sum([Score])  from Class group by [Student]')--加多一列(学科平均分用avg([Score]))生成动态:select 
    [Student],
    [数学]=max(case when [Course]='数学' then [Score] else 0 end),
    [物理]=max(case when [Course]='物理' then [Score] else 0 end),
    [英语]=max(case when [Course]='英语' then [Score] else 0 end),
    [语文]=max(case when [Course]='语文' then [Score] else 0 end),
    [总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
    from 
    Class 
    group by [Student]go--2005方法:动态:declare @s nvarchar(4000)
    Select  @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
    exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a 
    pivot (max([Score]) for [Course] in('+@s+'))b ')生成静态:select 
    [Student],[数学],[物理],[英语],[语文],[总成绩] 
    from 
    (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
    pivot 
    (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b 生成格式:/*
    Student 数学          物理          英语          语文          总成绩
    ------- ----------- ----------- ----------- ----------- -----------
    李四      77          85          65          65          292
    张三      87          90          82          78          337(2 行受影响)
    */go--2、列转行
    --> --> (Roy)生成測試數據
     
    if not object_id('Class') is null
    drop table Class
    Go
    Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
    Insert Class
    select N'李四',77,85,65,65 union all
    select N'张三',87,90,82,78
    Go--2000:动态:declare @s nvarchar(4000)
    select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
    +',[Score]='+quotename(Name)+' from Class'
    from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
    order by Colid
    exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序生成静态:
    select * 
    from (select [Student],[Course]='数学',[Score]=[数学] from Class union all 
    select [Student],[Course]='物理',[Score]=[物理] from Class union all 
    select [Student],[Course]='英语',[Score]=[英语] from Class union all 
    select [Student],[Course]='语文',[Score]=[语文] from Class)t 
    order by [Student],[Course]go
    --2005:动态:declare @s nvarchar(4000)
    select @s=isnull(@s+',','')+quotename(Name)
    from syscolumns where ID=object_id('Class') and Name not in('Student') 
    order by Colid
    exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')go
    select 
    Student,[Course],[Score] 
    from 
    Class 
    unpivot 
    ([Score] for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
    /*
    Student Course Score
    ------- ------- -----------
    李四      数学      77
    李四      物理      85
    李四      英语      65
    李四      语文      65
    张三      数学      87
    张三      物理      90
    张三      英语      82
    张三      语文      78(8 行受影响)
    */
      

  2.   

    select  a.名称, a.通道一Max,b.时间,a.通道一Min,c.时间,a.通道二Max,d.时间,a.通道二min,e.时间 
    from 
    (select 名称,通道一Max=max(通道一),通道一Min=min(通道一),通道二Max =max(通道二),通道二min=min(通道二)
    group by 名称) a
    left join ta b on a.名称=b.名称 and a.通道一Max=b.通道一
    left join ta c on a.名称=c.名称 and a.通道一Min=b.通道一
    left join ta d on a.名称=d.名称 and a.通道二Max=b.通道二
    left join ta e on a.名称=e.名称 and a.通道二min=b.通道二
      

  3.   


    declare @t table(名称 int, 日期 char(10), 时间 char(8), 通道一 decimal(4, 2), 通道二 decimal(4,2))
    insert @t
    select 1, '2006-03-06',  '00:01:08', 0.42,     0.20 union all
    select 2, '2006-03-06',  '00:02:11', 0.43,     0.10 union all
    select 3, '2006-03-06',  '00:03:14', 0.44,     0.10 union all
    select 1, '2006-03-06',  '00:04:17', 0.44,     0.20 union all
    select 2, '2006-03-06',  '00:05:21', 0.41,     0.20 union all
    select 3, '2006-03-06',  '00:06:24', 0.41,     0.10 union all
    select 1, '2006-03-06',  '00:07:27', 0.44,     0.00 union all
    select 2, '2006-03-06',  '00:08:30', 0.42,     0.10 union all
    select 3, '2006-03-06',  '00:09:33', 0.44,     0.00 union all
    select 1, '2006-03-06',  '00:10:36', 0.40,     0.20 union all
    select 2, '2006-03-06',  '00:11:40', 0.44,     0.20 union all
    select 3, '2006-03-06',  '00:12:43', 0.44,     0.30 union all
    select 1, '2006-03-06',  '00:13:46', 0.42,     0.10 union all
    select 2, '2006-03-06',  '00:14:49', 0.42,     0.20 union all
    select 3, '2006-03-06',  '00:15:52', 0.45,     0.30 union all
    select 1, '2006-03-06',  '00:16:55', 0.42,     0.10 union all
    select 2, '2006-03-06',  '00:17:59', 0.41,     0.30 union all
    select 3, '2006-03-06',  '00:19:02', 0.42,     0.50 
    select a.名称, a.通道一Max, min(b.时间), a.通道一Min, min(c.时间), a.通道二Max, min(d.时间), a.通道二Min, min(e.时间) from
    (select 名称, max(通道一) as 通道一Max, min(通道一) as 通道一Min, max(通道二) as 通道二Max, min(通道二) as 通道二min
      from @t group by 名称) a
    join @t b on a.名称= b.名称 and a.通道一Max = b.通道一
    join @t c on a.名称= c.名称 and a.通道一Min = c.通道一
    join @t d on a.名称= d.名称 and a.通道二Max = d.通道二
    join @t e on a.名称= e.名称 and a.通道二Min = e.通道二
    group by a.名称, a.通道一Max, a.通道一Min, a.通道二Max, a.通道二Min
    /*
    名称          通道一Max          通道一Min          通道二Max          通道二Min          
    ----------- ------ -------- ------ -------- ------ -------- ------ -------- 
    1           .44    00:04:17 .40    00:10:36 .20    00:01:08 .00    00:07:27
    2           .44    00:11:40 .41    00:05:21 .30    00:17:59 .10    00:02:11
    3           .45    00:15:52 .41    00:06:24 .50    00:19:02 .00    00:09:33
    */
      

  4.   

    加上日期declare @t table(名称 int, 日期 char(10), 时间 char(8), 通道一 decimal(4, 2), 通道二 decimal(4,2))
    insert @t
    select 1, '2006-03-06',  '00:01:08', 0.42,     0.20 union all
    select 2, '2006-03-06',  '00:02:11', 0.43,     0.10 union all
    select 3, '2006-03-06',  '00:03:14', 0.44,     0.10 union all
    select 1, '2006-03-06',  '00:04:17', 0.44,     0.20 union all
    select 2, '2006-03-06',  '00:05:21', 0.41,     0.20 union all
    select 3, '2006-03-06',  '00:06:24', 0.41,     0.10 union all
    select 1, '2006-03-06',  '00:07:27', 0.44,     0.00 union all
    select 2, '2006-03-06',  '00:08:30', 0.42,     0.10 union all
    select 3, '2006-03-06',  '00:09:33', 0.44,     0.00 union all
    select 1, '2006-03-06',  '00:10:36', 0.40,     0.20 union all
    select 2, '2006-03-06',  '00:11:40', 0.44,     0.20 union all
    select 3, '2006-03-06',  '00:12:43', 0.44,     0.30 union all
    select 1, '2006-03-06',  '00:13:46', 0.42,     0.10 union all
    select 2, '2006-03-06',  '00:14:49', 0.42,     0.20 union all
    select 3, '2006-03-06',  '00:15:52', 0.45,     0.30 union all
    select 1, '2006-03-06',  '00:16:55', 0.42,     0.10 union all
    select 2, '2006-03-06',  '00:17:59', 0.41,     0.30 union all
    select 3, '2006-03-06',  '00:19:02', 0.42,     0.50 
    select a.名称, a.日期, a.通道一Max, min(b.时间) as 时间, a.通道一Min, min(c.时间) as 时间, a.通道二Max, min(d.时间) as 时间, a.通道二Min, min(e.时间) as 时间 from
    (select 名称, 日期, max(通道一) as 通道一Max, min(通道一) as 通道一Min, max(通道二) as 通道二Max, min(通道二) as 通道二min
      from @t group by 名称, 日期) a
    join @t b on a.名称= b.名称 and a.通道一Max = b.通道一
    join @t c on a.名称= c.名称 and a.通道一Min = c.通道一
    join @t d on a.名称= d.名称 and a.通道二Max = d.通道二
    join @t e on a.名称= e.名称 and a.通道二Min = e.通道二
    group by a.名称, a.日期, a.通道一Max, a.通道一Min, a.通道二Max, a.通道二Min
    /*
    名称          日期         通道一Max 时间       通道一Min 时间       通道二Max 时间       通道二Min 时间       
    ----------- ---------- ------ -------- ------ -------- ------ -------- ------ -------- 
    1           2006-03-06 .44    00:04:17 .40    00:10:36 .20    00:01:08 .00    00:07:27
    2           2006-03-06 .44    00:11:40 .41    00:05:21 .30    00:17:59 .10    00:02:11
    3           2006-03-06 .45    00:15:52 .41    00:06:24 .50    00:19:02 .00    00:09:33
    */