sql聚合,总跳不出这个坎

luomingliang1026 2011-12-14 08:38:39
select
fw.fund_id
--,fw.statistic_date
,min(abs(datediff(day,fw.statistic_date,'2010-11-30'))) dif
from t_fund_week_statistic fw
where fw.statistic_date between '2010-11-23' and '2010-12-07'
group by fw.fund_id
order by fund_id
大家帮忙看下上面的程序,我想对fund_id聚合,得到最靠近11月底的时间点,但有需要输出哪个时间点fw.statistic_date,
假如一起聚合fund_id和fw.statistic_date结果又不是单个fund_id对于一个fw.statistic_date

怎么处理啊,跪求高手!!
...全文
48 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
luomingliang1026 2012-01-09
  • 打赏
  • 举报
回复
这样岂不跟fund_id无关了
luomingliang1026 2012-01-09
  • 打赏
  • 举报
回复
都不对,一个日期要与一个fund_id对应的
--小F-- 2011-12-14
  • 打赏
  • 举报
回复
select
*
from
t_fund_week_statistic t
where
statistic_date between '2010-11-23' and '2010-12-07'
and
abs(datediff(s,statistic_date,'2010-12-01'))=(select min( abs(datediff(s,statistic_date,'2010-12-01'))) from t_fund_week_statistic fund_id
=t.fund_id)
-晴天 2011-12-14
  • 打赏
  • 举报
回复
select * from t_fund_week_statistic a
where statistic_date between '2010-11-23' and '2010-12-07'
and not exists(select 1 from t_fund_week_statistic where abs(datediff(s,statistic_date,'2010-12-01'))<abs(datediff(s,a.statistic_date,'2010-12-01')))

34,588

社区成员

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

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