订单编号,创建时间,用户id,产品内容。
001 2010/10/2 ZS 手表
001 2010/10/2 LS 手机
002 2010/10/2 ZS 电池
002 2010/10/2 WW 手表
002 2010/10/2 LL 手机
003 2010/10/2 QQ 打火机
003 2010/10/2 ZS 书问题如下;
我只想取出每订单订的第一行。设想查询后结果是:001 2010/10/2 ZS 手表
002 2010/10/2 ZS 电池
003 2010/10/2 QQ 打火机我用distinct 订单编号,创建时间,用户id,产品内容 没有用,因为 用户名是不重复的!
求解答!!!!
001 2010/10/2 ZS 手表
001 2010/10/2 LS 手机
002 2010/10/2 ZS 电池
002 2010/10/2 WW 手表
002 2010/10/2 LL 手机
003 2010/10/2 QQ 打火机
003 2010/10/2 ZS 书问题如下;
我只想取出每订单订的第一行。设想查询后结果是:001 2010/10/2 ZS 手表
002 2010/10/2 ZS 电池
003 2010/10/2 QQ 打火机我用distinct 订单编号,创建时间,用户id,产品内容 没有用,因为 用户名是不重复的!
求解答!!!!
FROM ( SELECT * ,
rn = row_number() OVER ( PARTITION BY 订单编号 ORDER BY ( SELECT
1
) )
FROM tb
) AS D
WHERE rn = 1
设想中的查询结果001 2010/10/2 ZS 手表
002 2010/10/2 ZS 电池
003 2010/10/2 ZS 书
拜托了!!!!!!
如果是条件
在里面
from tb
where 用户id='Zs'
drop table t1
go
create table t1(订单编号 varchar(10),创建时间 datetime,用户id varchar(10),产品内容 varchar(20))insert t1
select '001', '2010/10/2', 'ZS', '手表' union all
select '001', '2010/10/2', 'LS', '手机' union all
select '002', '2010/10/2', 'ZS', '电池' union all
select '002', '2010/10/2', 'WW', '手表' union all
select '002', '2010/10/2', 'LL', '手机' union all
select '003', '2010/10/2', 'QQ', '打火机' union all
select '003', '2010/10/2', 'ZS', '书'
go
select * from t1 a where not exists
(select 1 from t1 b where a.[产品内容]=b.[产品内容] and a.[订单编号]<>b.[订单编号])
/*
订单编号 创建时间 用户id 产品内容
------ --- --- -----
002 2010-10-02 00:00:00.000 ZS 电池
003 2010-10-02 00:00:00.000 QQ 打火机
003 2010-10-02 00:00:00.000 ZS 书
(3 行受影响)
*/
go
drop table t1
......select * from t1 where 用户id='ZS'
if object_id('tb') is not null
drop table tb
go
create table tb
(
订单编号 varchar(10),
创建时间 varchar(10),
用户id varchar(10),
产品内容 varchar(10)
)
go
insert into tb
select '001','2010/10/2','ZS','手表' union all
select '001','2010/10/2','LS','手机' union all
select '002','2010/10/2','ZS','电池' union all
select '002','2010/10/2','WW','手表' union all
select '002','2010/10/2','LL','手机' union all
select '003','2010/10/2','QQ','打火机' union all
select '003','2010/10/2','ZS','书'
go
select 订单编号,创建时间,用户id,产品内容 from
(
select *,row=row_number() over(partition by 订单编号 order by getdate()) from tb
)t where row=1
go
/*
订单编号 创建时间 用户id 产品内容
---------- ---------- ---------- ----------
001 2010/10/2 ZS 手表
002 2010/10/2 ZS 电池
003 2010/10/2 QQ 打火机(3 行受影响)
*/
if object_id('tb') is not null
drop table tb
go
create table tb
(
订单编号 varchar(10),
创建时间 varchar(10),
用户id varchar(10),
产品内容 varchar(10)
)
go
insert into tb
select '001','2010/10/2','ZS','手表' union all
select '001','2010/10/2','LS','手机' union all
select '002','2010/10/2','ZS','电池' union all
select '002','2010/10/2','WW','手表' union all
select '002','2010/10/2','LL','手机' union all
select '003','2010/10/2','QQ','打火机' union all
select '003','2010/10/2','ZS','书'
go
select 订单编号,创建时间,用户id,产品内容 from
(
select *,row=row_number() over(partition by 订单编号 order by getdate()) from tb
)t where row=1
go
--只查询ZS的订单行
select * from tb where 用户id='ZS'
/*
订单编号 创建时间 用户id 产品内容
---------- ---------- ---------- ----------
001 2010/10/2 ZS 手表
002 2010/10/2 ZS 电池
003 2010/10/2 ZS 书(3 行受影响)*/
from tb
where 用户id = 'zs'
group by 产品内容
from tb
where 用户id = 'zs'
group by 产品内容