读书人

疑难SQL语句

发布时间: 2013-03-01 18:33:02 作者: rapoo

疑难SQL语句 请教高手
这个是表内容
疑难SQL语句

我想做一个查询
SELECT ID,编号,日期, 数量,MAX(日期2), (-这里加上MAX(日期2) 对应的ID2-) from T
GROUP by ID,编号,日期, 数量

这个有办法实现吗?
[解决办法]
select ID,编号,日期, 数量,[最大日期],ID2 from T where 日期2 in(
SELECT [最大日期]from(
SELECT ID,编号,日期, 数量,[最大日期]=MAX(日期2) from T
GROUP by ID,编号,日期, 数量
)a
)
[解决办法]

SELECT ID,编号,日期, 数量,日期2,ID2 from T a
where not exists (select 1 from T where ID=a.ID and 日期2>a.日期2)

[解决办法]
SELECT ID,编号,日期, 数量,日期2,ID2
from T a
where not exists (
select 1
from T
where ID=a.ID
and 编号 = a.编号
and 日期 = a.日期
and 数量 = a.数量
and 日期2>a.日期2
)

读书人网 >SQL Server

热点推荐