简单问题 有分散

xingxing2378 2006-10-21 10:29:28
一张表
id name point data
1 a 5 2006
1 a 5 2005
1 a 5 2004
1 a 5 2006
1 a 5 2005
1 a 5 2006

如果data 是2006年的话取此数据如果不是的话 取其他年度中 point 最大的那条
...全文
165 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
十一月猪 2006-10-21
  • 打赏
  • 举报
回复
select id , name , point , data from t where data = '2006'
union
select id , name , point , data from t a where data <>'2006'
and not exists (select 1 from t where data = a.data and point > a.point)
xiaoku 2006-10-21
  • 打赏
  • 举报
回复
select id ,name ,case when date ='2006' then point
else (select (max(point) from tb where id =a.id and name =a.name and data =a.date ) end as point,
,date
from tb a

这样不知对不对?
早起晚睡 2006-10-21
  • 打赏
  • 举报
回复
if (select count(*) from 表1 where data='2006')<0

begin
select a.id,a.name,a.point,a.data from 表1 a inner join (select id,max(point) as point from 表1 group by id) b
on
a.id=b.id
end
else
select * from 表1 where data='2006'
hhhdyj 2006-10-21
  • 打赏
  • 举报
回复
问题说得不清楚,俺这样理解的,不知对不对

declare @t table(id int, name varchar(10), point int, data int)
insert into @t values(1, 'a' , 5, 2006)
insert into @t values(1, 'a' , 4, 2005)
insert into @t values(1, 'a' , 3, 2004)
insert into @t values(2, 'b' , 2, 2006)
insert into @t values(3, 'c' , 1, 2005)
insert into @t values(3, 'c' , 7, 2004)

select id, name, point from @t where data = 2006
union
select a.id, a.name, max(point) as point
from @t a
left join (select id from @t where data = 2006)b
on a.id = b.id
where b.id is null
group by a.id, a.name
xiaoku 2006-10-21
  • 打赏
  • 举报
回复
取其他年度中 point 最大的那条

?要取分类吗?最大哪条
九斤半 2006-10-21
  • 打赏
  • 举报
回复
如果data 是2006年的话取此数据如果不是的话 取其他年度中 point 最大的那条

不明白啊不明白~~~
xingxing2378 2006-10-21
  • 打赏
  • 举报
回复
条件是不能用函数 只能用sql语句阿

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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