作者grence (多想两分钟 = =")
看板Database
标题Re: [SQL ] 筛选资料的语法
时间Mon Mar 22 23:24:24 2010
※ 引述《chengchieh (chieh)》之铭言:
: tablea
: ca为某地点的id...另一张表的pk
: cb为该笔资料的记录时间
: cc~cj为杂鱼...
select t1.*
from tablea t1
join (
select ca, max(cb)max_cb, max(cc)cc... --add other columns to make PK
from tablea
group by ca
)t2 on t1.ca=t2.ca and t1.cb=t2.max_cb and t1.cc=t2.cc...
缺点:max(cb)、max(cc)可能属於不同笔资料。
select t1.*, t2.*
from tablea t1
cross apply(
select top 3 * -- top 3...只是让效果比较容易观察
from tablea t2
where t1.ca=t2.ca
order by t2.cb desc
)t2
缺点:只确定 SQL Server能跑。
以上,未经测试(没有资料怎麽测…)
但看懂概念应该很好改。
: 需求为取出 不重复的ca的最新一笔资料(一个ca只能输出1笔)
: 有使用过 distinct ca, max(cb) 下去做过...
: but...在掺入了其他杂鱼之後
: 因为有不少资料是 ca一样..cb也一样(同时间同单位不同资料内容)
: 出来的东西就跟我本来要的条件相反了...
: 不知道是不是有什麽方法/语法可以推荐呢?
: ps...资料表的schema不能动 > <
: 描述的可能不是很好...希望有人能帮忙一下~~
: thx~
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.194.46.184