求sql语句

zhui22222 2012-02-03 04:15:19
现有2个表
表1:全公司人员名单
表2:公司部分人员奖金登记(同一人不同日期可多次登记)、发放奖金日期

求在某时间段内,全公司人员奖金统计的sql语句(未发奖金的人奖金显示为0)
...全文
93 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2012-02-03
  • 打赏
  • 举报
回复
说明你的A表和B表中都有Name字段。
zhui22222 2012-02-03
  • 打赏
  • 举报
回复
不嵌套的可以了,前面那个报错多次为aa指定了列 name
叶子 2012-02-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhui22222 的回复:]
引用 2 楼 maco_wang 的回复:
SQL code

declare @表1 table (id int,name varchar(4))
insert into @表1
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'

declare @表2 table (nameid int,奖金 int,……
[/Quote]


--不嵌套也是可以的
select name,sum(isnull(奖金,0)) as 奖金
from @表1 a full join @表2 b
on a.id=b.nameid and 发放奖金日期 between '20120102' and '20120103'
where a.id is not null group by name
zhui22222 2012-02-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code

declare @表1 table (id int,name varchar(4))
insert into @表1
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'

declare @表2 table (nameid int,奖金 int,发放奖金日期 datetime)
inser……
[/Quote]
应该就是这个了
叶子 2012-02-03
  • 打赏
  • 举报
回复

declare @表1 table (id int,name varchar(4))
insert into @表1
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'

declare @表2 table (nameid int,奖金 int,发放奖金日期 datetime)
insert into @表2
select 1,500,'20120101' union all
select 2,600,'20120102' union all
select 2,700,'20120102' union all
select 3,800,'20120103' union all
select 3,1000,'20120104' union all
select 3,900,'20120104'

select name,sum(isnull(奖金,0)) as 奖金 from
(
select * from @表1 a full join @表2 b on a.id=b.nameid
and 发放奖金日期 between '20120102' and '20120103'
where a.id is not null
) aa group by name
/*
name 奖金
---- -----------
李四 1300
王五 800
张三 0
*/
勿勿 2012-02-03
  • 打赏
  • 举报
回复
关联求和

27,582

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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