公司名 订单号 订单时间
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===================================================================我现在想做的 是 根据 订单时间 降序排列
显示 三个字段的内容, 筛选出 公司重名信息至于订单号 能显示出来就行,就是说 显示 哪条 公司的信息,后面就跟着显示那个订单号就行
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===================================================================我现在想做的 是 根据 订单时间 降序排列
显示 三个字段的内容, 筛选出 公司重名信息至于订单号 能显示出来就行,就是说 显示 哪条 公司的信息,后面就跟着显示那个订单号就行
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
--是这个吗?
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 行受影响)
--或者
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 行受影响)
我是把sql语句用到 asp里的!请大家帮我看看吧!谢谢