select * from t aa
where (select count(*) from t where 规格=aa.规格 and 日期<=aa.日期)=3
where (select count(*) from t where 规格=aa.规格 and 日期<=aa.日期)=3
解决方案 »
- [sql server 2008 r2 同一个镜像出现两种语言的问题]
- 处女贴
- 请问一下表A中漏填的内容用哪个语句可以等于表B的内容
- SELECT 简单的语句
- 请教几个关于实例,数据库,数据文件,登录名,数据库用户名,模式(schema),表的概念问题
- 如何用列序号选择特定列
- 表中的一个字段取多个值的问题
- 如何使SQL SERVER中的 SET DATEFIRST 1 设为默认值?
- 如何在存储过程中拼凑condition(是高手的请近来)
- 再帮一下
- 同一服务器上的两个数据库怎么移啊?
- select * from table where field = ???? ,表field列允许空,所以怎么查询查询 where field 后面怎么写?是空值的话/
where (select count(*) from t where 规格=aa.规格 and 日期<=aa.日期)<=3 --前三个,应该是
select * from t
where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 order by 日期)
select * from t aa
where (select count(*) from t where 规格=aa.规格 and 日期<=aa.日期)<=3
select * from t
where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 order by 日期)
挺好的
select * from t
where 日期 in(select top 3 with ties 日期 from t as a where t.规格=a.规格 order by 日期)
insert into tab select
'电脑', 'A1', 2, '2004-3-3' union all select
'电脑', 'A1', 3, '2004-4-4' union all select
'电脑', 'A1', 4, '2004-5-5' union all select
'电脑', 'A1', 10, '2004-6-6' union all select
'电脑', 'A2', 5, '2004-3-3' union all select
'电脑', 'A2', 6, '2004-4-4' union all select
'电脑', 'A2', 7, '2004-5-5' union all select
'电脑', 'A2', 8, '2004-6-6' union all select
'电脑', 'A4', 5, '2004-3-3' union all selectselect * from tab a where a.日期 in(select top 3 b.日期 from tab b where a.规格=b.规格) 名称 规格 单价 日期
------ ---- --------------------- ------------------------------------------------------
电脑 A1 2.0000 2004-03-03 00:00:00.000
电脑 A1 3.0000 2004-04-04 00:00:00.000
电脑 A1 4.0000 2004-05-05 00:00:00.000
电脑 A2 5.0000 2004-03-03 00:00:00.000
电脑 A2 6.0000 2004-04-04 00:00:00.000
电脑 A2 7.0000 2004-05-05 00:00:00.000
电脑 A4 5.0000 2004-03-03 00:00:00.000
电脑 A4 6.0000 2004-04-04 00:00:00.000
电脑 A4 7.0000 2004-05-05 00:00:00.000(所影响的行数为 9 行)
select * from t
where 日期 in(select top 3 日期 from t as a where t.规格=a.规格 group by 日期)
create table tab(名称 char(6),规格 char(4),单价 money,日期 datetime)
insert into tab select
'电脑', 'A1', 2, '2004-3-3' union all select
'电脑', 'A1', 3, '2004-4-4' union all select
'电脑', 'A1', 4, '2004-5-5' union all select
'电脑', 'A1', 10, '2004-6-6' union all select
'电脑', 'A2', 5, '2004-3-3' union all select
'电脑', 'A2', 6, '2004-4-4' union all select
'电脑', 'A2', 7, '2004-5-5' union all select
'电脑', 'A2', 8, '2004-6-6' union all select
'电脑', 'A4', 5, '2004-3-3' union all select
'电脑', 'A4', 6, '2004-4-4' union all select
'电脑', 'A4', 7, '2004-5-5' union all select
'电脑', 'A4', 8, '2004-6-6'
select * from tab a where a.日期 in(select top 3 b.日期 from tab b where a.规格=b.规格 order by b.日期)名称 规格 单价 日期
------ ---- --------------------- ------------------------------------------------------
电脑 A1 2.0000 2004-03-03 00:00:00.000
电脑 A1 3.0000 2004-04-04 00:00:00.000
电脑 A1 4.0000 2004-05-05 00:00:00.000
电脑 A2 5.0000 2004-03-03 00:00:00.000
电脑 A2 6.0000 2004-04-04 00:00:00.000
电脑 A2 7.0000 2004-05-05 00:00:00.000
电脑 A4 5.0000 2004-03-03 00:00:00.000
电脑 A4 6.0000 2004-04-04 00:00:00.000
电脑 A4 7.0000 2004-05-05 00:00:00.000(所影响的行数为 9 行)
电脑 A1 44.0000 2004-03-03 00:00:00.000 --日期相同的
电脑 A1 4.0000 2004-05-05 00:00:00.000那么这样也算最早的 注意单价不同
where 日期 in
(select top 3 with ties 日期 from Table1 t where table1.guige=t.guige order by 日期)
order by 规格
drop table [dbo].[temp1]if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[temp2]create table temp1(名稱 varchar(20) not null,規格 varchar(20) not null,單價 int not null,日期 smalldatetime not null)
create table temp2(名稱 varchar(20) not null,規格 varchar(20) not null,單價 int not null,日期 smalldatetime not null)
goinsert into temp1
select '電腦', 'A1', 2, '2004-3-3' union all
select '電腦', 'A1', 3, '2004-4-4' union all
select '電腦', 'A1', 4, '2004-5-5' union all
select '電腦', 'A1', 10, '2004-6-6' union all
select '電腦', 'A2', 5, '2004-3-3' union all
select '電腦', 'A2', 6, '2004-4-4' union all
select '電腦', 'A2', 7, '2004-5-5' union all
select '電腦', 'A2', 8, '2004-6-6' union all
select '電腦', 'A4', 5, '2004-3-3' union all
select '電腦', 'A4', 6, '2004-4-4' union all
select '電腦', 'A4', 7, '2004-5-5' union all
select '電腦', 'A4', 8, '2004-6-6' insert into temp2
select '電腦', 'A1', 2, '2004-3-3' union all
select '電腦', 'A1', 3, '2004-4-4' union all
select '電腦', 'A1', 4, '2004-5-5' union all
select '電腦', 'A1', 10, '2004-6-6' union all
select '電腦', 'A2', 5, '2004-3-3' union all
select '電腦', 'A2', 6, '2004-4-4' union all
select '電腦', 'A2', 7, '2004-5-5' union all
select '電腦', 'A2', 8, '2004-6-6' union all
select '電腦', 'A2', 9, '2004-6-6' union all
select '電腦', 'A4', 5, '2004-3-3' union all
select '電腦', 'A4', 6, '2004-4-4' union all
select '電腦', 'A4', 7, '2004-5-5' union all
select '電腦', 'A4', 7, '2004-5-5' union all
select '電腦', 'A4', 8, '2004-6-6'
goselect * from temp1
/*
名稱 規格 單價 日期
電腦 A1 2 2004-03-03 00:00:00
電腦 A1 3 2004-04-04 00:00:00
電腦 A1 4 2004-05-05 00:00:00
電腦 A1 10 2004-06-06 00:00:00
電腦 A2 5 2004-03-03 00:00:00
電腦 A2 6 2004-04-04 00:00:00
電腦 A2 7 2004-05-05 00:00:00
電腦 A2 8 2004-06-06 00:00:00
電腦 A4 5 2004-03-03 00:00:00
電腦 A4 6 2004-04-04 00:00:00
電腦 A4 7 2004-05-05 00:00:00
電腦 A4 8 2004-06-06 00:00:00*/select * from temp2
/*
名稱 規格 單價 日期
電腦 A1 2 2004-03-03 00:00:00
電腦 A1 3 2004-04-04 00:00:00
電腦 A1 4 2004-05-05 00:00:00
電腦 A1 10 2004-06-06 00:00:00
電腦 A2 5 2004-03-03 00:00:00
電腦 A2 6 2004-04-04 00:00:00
電腦 A2 7 2004-05-05 00:00:00
電腦 A2 8 2004-06-06 00:00:00
電腦 A2 9 2004-06-06 00:00:00
電腦 A4 5 2004-03-03 00:00:00
電腦 A4 6 2004-04-04 00:00:00
電腦 A4 7 2004-05-05 00:00:00
電腦 A4 7 2004-05-05 00:00:00
電腦 A4 8 2004-06-06 00:00:00*/--------------------(名稱+規格+日期唯一)-------------------------------
--方法一
select * from temp1 a
where (select count(*) from temp1 where a.名稱=名稱 and a.規格=規格 and a.日期>日期)<3
/*
名稱 規格 單價 日期
電腦 A1 2 2004-03-03 00:00:00
電腦 A1 3 2004-04-04 00:00:00
電腦 A1 4 2004-05-05 00:00:00
電腦 A2 5 2004-03-03 00:00:00
電腦 A2 6 2004-04-04 00:00:00
電腦 A2 7 2004-05-05 00:00:00
電腦 A4 5 2004-03-03 00:00:00
電腦 A4 6 2004-04-04 00:00:00
電腦 A4 7 2004-05-05 00:00:00*/--方法二
select * from temp1 a
where 日期 in (select top 3 日期 from temp1 where a.名稱=名稱 and a.規格=規格 order by 日期)
/*
名稱 規格 單價 日期
電腦 A1 2 2004-03-03 00:00:00
電腦 A1 3 2004-04-04 00:00:00
電腦 A1 4 2004-05-05 00:00:00
電腦 A2 5 2004-03-03 00:00:00
電腦 A2 6 2004-04-04 00:00:00
電腦 A2 7 2004-05-05 00:00:00
電腦 A4 5 2004-03-03 00:00:00
電腦 A4 6 2004-04-04 00:00:00
電腦 A4 7 2004-05-05 00:00:00*/
--------------------(名稱+規格+日期不唯一)-------------------------------
alter table temp2 add id int not null identity(1,1)
goselect a.名稱,a.規格,a.單價,a.日期 from temp2 a
where id in (select top 3 id from temp2 where a.名稱=名稱 and a.規格=規格 order by 日期)
/*
名稱 規格 單價 日期
電腦 A1 2 2004-03-03 00:00:00
電腦 A1 3 2004-04-04 00:00:00
電腦 A1 4 2004-05-05 00:00:00
電腦 A2 5 2004-03-03 00:00:00
電腦 A2 6 2004-04-04 00:00:00
電腦 A2 7 2004-05-05 00:00:00
電腦 A4 5 2004-03-03 00:00:00
電腦 A4 6 2004-04-04 00:00:00
電腦 A4 7 2004-05-05 00:00:00*/alter table temp2 drop column id