大家能否看看这个SQL有可能实现吗?

jerry_huang 2003-12-03 08:50:48
源表A:
Name Position
Jerry IT
Tom IT
Jack IT
Sherry HR
Diana HR
Echo HR

源表B:
Position Budget
IT 4
HR 2

连接后的表C:
Name Position Budget
Jerry IT 4
Tom IT 0
Jack IT 0
Sherry HR 2
Diana HR 0
Echo HR 0

也就是说连接两个表以后,我只想第一条记录取到另一个表的值,也就是说只有第一条IT记录取得Budget=4,而其它都取0,HR也是一样,第一条取得2,其余取0,SQL能这样实现吗?

我只有十几分了,只能给这么多了,不好意思!
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
playyuer 2003-12-03
  • 打赏
  • 举报
回复
select *
,isnull((select sum(Budget)
from b
where Position = a.Position
and a.Name = (select top 1 name
from a c
where Position = a.Position
order by name),0)
)
from A


select *
,isnull((select sum(Budget)
from b
where Position = a.Position
and a.Name = (select max(name) from a c where Position = a.Position)
),0)
from A
zarge 2003-12-03
  • 打赏
  • 举报
回复
select A.name, A.position, case when A.name = (select top 1 name from A where position = B.position) then B.budget else 0 end from A inner join B on A.position = B.position
playyuer 2003-12-03
  • 打赏
  • 举报
回复
select *
,(select sum(Budget)
from b
where Position = a.Position
and a.Name = (select max(name) from a c where Position = a.Position)
)
from A
a_stupid_boy 2003-12-03
  • 打赏
  • 举报
回复
好怪的需求啊,搞不明白

34,874

社区成员

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

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