求高手相助,这个语句怎么写
求高手相助,这个语句如何写
SQL2000表TEST:
年度 修改时间 数量1, 数量2
2015 2015-2-4 100 120
2015 2015-9-9 80 70
2014 2014-12-20 90 220
2014 2014-8-22 80 112
2014 2014-7-11 78 210
2013 2013-6-7 120 30
如想查询2014年1月日以后每一年数据中最新记录的合计数,如何写语句能得到结果为:
最早年度:2014,数量1:170,数量2:290
(即第2条记录2015 2015-9-9 80 70
和第3条记录2014 2014-12-20 90 220
的合计数)
------解决思路----------------------
我看错需求了。
select SUM(数量1) as 数量1,SUM( 数量2) as 数量2 from Table_1
where 修改时间 in (select 修改时间 from Table_1 a where 修改时间与查询时间条件 and 修改时间 in
(select top 1 修改时间 from Table_1 b where a.年度=b.年度 order by date desc))
这条SQL可以满足你的需求
------解决思路----------------------
测试通过!
select sum(sl1) as hj1,sum(sl2) as hj2 from b
where xgsj in(
select max(xgsj)
from b where xgsj>'2014-01-31' group by nd)
即
select sum(数量1) as 合计1,sum(数量2) as 合计2 from TEST
where 修改时间 in (
select max(修改时间) from TEST where 修改时间>'2014-01-31' gorup by 年度)
SQL2000表TEST:
年度 修改时间 数量1, 数量2
2015 2015-2-4 100 120
2015 2015-9-9 80 70
2014 2014-12-20 90 220
2014 2014-8-22 80 112
2014 2014-7-11 78 210
2013 2013-6-7 120 30
如想查询2014年1月日以后每一年数据中最新记录的合计数,如何写语句能得到结果为:
最早年度:2014,数量1:170,数量2:290
(即第2条记录2015 2015-9-9 80 70
和第3条记录2014 2014-12-20 90 220
的合计数)
------解决思路----------------------
我看错需求了。
select SUM(数量1) as 数量1,SUM( 数量2) as 数量2 from Table_1
where 修改时间 in (select 修改时间 from Table_1 a where 修改时间与查询时间条件 and 修改时间 in
(select top 1 修改时间 from Table_1 b where a.年度=b.年度 order by date desc))
这条SQL可以满足你的需求
------解决思路----------------------
测试通过!
select sum(sl1) as hj1,sum(sl2) as hj2 from b
where xgsj in(
select max(xgsj)
from b where xgsj>'2014-01-31' group by nd)
即
select sum(数量1) as 合计1,sum(数量2) as 合计2 from TEST
where 修改时间 in (
select max(修改时间) from TEST where 修改时间>'2014-01-31' gorup by 年度)