问题求解。。

yinsuxia 2011-11-02 02:10:24
如何查出表中数据里面按月分,每个月的最大日期的那一天的所有数据?
表结构 :date originid usercount
就是要查出date列中每个月的最大日期,符合这个条件的记录。。。
...全文
54 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-02
  • 打赏
  • 举报
回复
select * from tb t where [date]=(select max([date]) from tb where datediff(mm,[date],t.[date])=0)
DataBox-MDX 2011-11-02
  • 打赏
  • 举报
回复


select tb.* from
(
select date,row_number(partition by month(date) order by date desc)as RN
from tb
) as tba
inner join tb on tba.RN=1 and tb.date=tba.date
-晴天 2011-11-02
  • 打赏
  • 举报
回复
or:
select * from tb a where not exists(select 1 from tb where year(date)=year(a.date) and month(date)=month(a.date) and date>a.date)
快溜 2011-11-02
  • 打赏
  • 举报
回复
select * from tb a
where [date]=(select max([date]) from tb
where convert(varchar(7),[date],120)=convert(varchar(7),a.[date],120))
-晴天 2011-11-02
  • 打赏
  • 举报
回复
select * from tb a where not exists(select 1 from tb where convert(varchar(7),date,120)=convert(varchar(7),a.date,120) and date>a.date)

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧