搞错了,是下面的内容:
表t1内容:
ID PROJECT VALUE DATE
0 1 30 2004-02-13
1 1 34 2004-02-27
2 2 43 2004-02-16
3 2 23 2004-02-30
。
。
。
请问如何写一条语句把每月每个项目最大日期的记录挑出来。
我要的结果是:
1 1 34 2004-02-27
3 2 23 2004-02-30
表t1内容:
ID PROJECT VALUE DATE
0 1 30 2004-02-13
1 1 34 2004-02-27
2 2 43 2004-02-16
3 2 23 2004-02-30
。
。
。
请问如何写一条语句把每月每个项目最大日期的记录挑出来。
我要的结果是:
1 1 34 2004-02-27
3 2 23 2004-02-30
解决方案 »
- 又是一个有关父子关系的SQL问题!求帮助
- 请问你下子查询问题..
- 按表table1里的字段cid=1查询表得来的数据,怎么把它链成一个字符串,,用sql 语名或者sql 自定义函数
- 我想在两台机器之间创建一触发器!
- 如何将SQLSERVER2005的数据库转成MYSQL
- 优化SQL语句时,下面关于大表(记录条数多的表)放在小表后面查询速度快的解释,对吗?
- 大家好
- 数据库还原问题,紧急求助!数据库 'person' 的媒体集有 2 个家族成员,但只提供了 1 个。必须提供所有的成员。
- 请大侠们帮个忙,定重谢!
- ODBC 访问错误!
- 如何给以下的变量赋值,多谢了
- 帮忙解释一下一个较复杂的SQL语句,谢谢!
insert into t1 select 0, 30, '2004-02-13'
insert into t1 select 1, 34, '2004-02-27'
insert into t1 select 2, 43, '2004-03-16'
insert into t1 select 3, 23, '2004-03-30'
select * from t1 where date in(select max(date)as date from t1 group by convert(varchar(7),date,120))
drop table t1ID VALUE DATE
----------- ----------- ------------------------------------------------------
1 34 2004-02-27 00:00:00.000
3 23 2004-03-30 00:00:00.000(所影响的行数为 2 行)
where DATE in
(
select max(DATE)
from tl
group by datepart(year,DATE),datepart(month,DATE)
)
(select 1 from t1 where
convert(char(7),[DATE],120)=convert(char(7),a.[DATE],120)
and [DATE]>a.[DATE]
and id=a.id )
select * from t1 a where not exists
(select 1 from t1 where
convert(char(7),[DATE],120)=convert(char(7),a.[DATE],120)
and [DATE]>a.[DATE]
and id=a.id )
insert into tb1
select 0, 30, '004-02-13'union all
select 1, 34, '2004-02-27'union all
select 2, 43, '2004-03-16'union all
select 3, 23, '2004-03-30'
select * into #1 from tb1
alter table #1 add char_date char (7)
go
update #1 set char_date=convert(char(7),[date],120)
select a.id,a.value,b.max_date from (select max_date=max(date) from #1 group by char_date)b left join tb1 a on a.date=b.max_date
drop table #1,tb1/*
id value max_date
----------- ----------- ------------------------------------------------------
1 34 2004-02-27 00:00:00.000
3 23 2004-03-30 00:00:00.000(所影响的行数为 2 行)
*/
create table tb1(id int ,value int,[date] datetime)
insert into tb1
select 0, 30, '004-02-13'union all
select 1, 34, '2004-02-27'union all
select 2, 43, '2004-03-16'union all
select 3, 23, '2004-03-30'
select a.id,a.value,b.date from (select date=max(date) from tb1 group by convert(varchar(7),date,120))b left join tb1 a on a.date=b.date
drop table tb1/*
id value max_date
----------- ----------- ------------------------------------------------------
1 34 2004-02-27 00:00:00.000
3 23 2004-03-30 00:00:00.000(所影响的行数为 2 行)
*/
insert into @a values (0,1,30,'2004-02-13')
insert into @a values (1,1,34,'2004-02-27')
insert into @a values (2,1,34,'2004-03-17')
insert into @a values (3,2,43,'2004-02-16')
insert into @a values (4,2,23,'2004-03-25')select * from @a where date in(select max(date) from @a group by project,Year(date),Month(date))显示结果
id project value date
----------- ----------- ----------- ------------------------------------------------------
1 1 34 2004-02-27 00:00:00.000
2 1 34 2004-03-17 00:00:00.000
3 2 43 2004-02-16 00:00:00.000
4 2 23 2004-03-25 00:00:00.000
先按project分组,再按date分组
select * from t1 a where
(select count(*) from t1 where month(a.date)=month(date) and a.date<date)=0