公司名         订单号             订单时间
  a             11111              2010-2-2
  a             12342              2010-3-2
  b             12341              2010-2-10
  a             13434              2010-2-2
  c             32341              2010-1-2
  b             34567              2010-1-10===================================================================我现在想做的 是  根据  订单时间 降序排列 
显示 三个字段的内容,  筛选出 公司重名信息至于订单号 能显示出来就行,就是说 显示 哪条 公司的信息,后面就跟着显示那个订单号就行

解决方案 »

  1.   

    --是要这样的结果吗?
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([公司名] varchar(1),[订单号] int,[订单时间] datetime)
    insert [TB]
    select 'a',11111,'2010-2-2' union all
    select 'a',12342,'2010-3-2' union all
    select 'b',12341,'2010-2-10' union all
    select 'a',13434,'2010-2-2' union all
    select 'c',32341,'2010-1-2' union all
    select 'b',34567,'2010-1-10'select  [公司名]=case when (select min([订单号]) from TB where t.[公司名]=[公司名])=[订单号] then [公司名] else '' end,
    [订单号],
    [订单时间]
    from [TB] t/*
    公司名  订单号         订单时间
    ---- ----------- -----------------------
    a    11111       2010-02-02 00:00:00.000
         12342       2010-03-02 00:00:00.000
    b    12341       2010-02-10 00:00:00.000
         13434       2010-02-02 00:00:00.000
    c    32341       2010-01-02 00:00:00.000
         34567       2010-01-10 00:00:00.000(6 行受影响)*/drop table TB
      

  2.   

    用分析函数GROUPING 
     
     
      

  3.   

    处理重复记录http://topic.csdn.net/u/20100305/03/569b6654-0248-4901-b6dc-2b310ddab9c0.html?30172
      

  4.   


    --是这个吗?
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([公司名] varchar(1),[订单号] int,[订单时间] datetime)
    go
    insert into [TB]
    select 'a',11111,'2010-2-2' union all
    select 'a',12342,'2010-3-2' union all
    select 'b',12341,'2010-2-10' union all
    select 'a',13434,'2010-2-2' union all
    select 'c',32341,'2010-1-2' union all
    select 'b',34567,'2010-1-10'select [公司名]=case when (select top 1 [订单号] from TB where t.[公司名]=[公司名] order by [订单时间])=[订单号] then [公司名] else '' end,
            [订单号],
            [订单时间]
    from [TB] t --结果:
    公司名  订单号         订单时间
    ---- ----------- -----------------------
    a    11111       2010-02-02 00:00:00.000
         12342       2010-03-02 00:00:00.000
         12341       2010-02-10 00:00:00.000
         13434       2010-02-02 00:00:00.000
    c    32341       2010-01-02 00:00:00.000
    b    34567       2010-01-10 00:00:00.000(6 行受影响)
      

  5.   


     --或者
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([公司名] varchar(1),[订单号] int,[订单时间] datetime)
    go
    insert into [TB]
    select 'a',11111,'2010-2-2' union all
    select 'a',12342,'2010-3-2' union all
    select 'b',12341,'2010-2-10' union all
    select 'a',13434,'2010-2-2' union all
    select 'c',32341,'2010-1-2' union all
    select 'b',34567,'2010-1-10'select * from 
    (
    select [公司名]=case when (select top 1 [订单号] from TB where t.[公司名]=[公司名] order by [订单时间])=[订单号] then [公司名] else '' end,
            [订单号],
            [订单时间]
    from [TB] t
    ) a
     where a.[公司名] <>''--结果
    公司名  订单号         订单时间
    ---- ----------- -----------------------
    a    11111       2010-02-02 00:00:00.000
    c    32341       2010-01-02 00:00:00.000
    b    34567       2010-01-10 00:00:00.000(3 行受影响)
      

  6.   

    不好意思,前几天太忙,忘回来看了
    我是把sql语句用到 asp里的!请大家帮我看看吧!谢谢