A表
品号,员工姓名(空)
B表
员工姓名,品号,产品数量在A表中提取第一行数据,判断,A.品号=B.品号 AND 产品数量最大 把B.员工姓名,插入到A.员工姓名
NET第二行,同上,但是要判定每个员工姓名不能在A表中超过3次!如果超过3次,选产品数量第二大的!!
求解!!!!!!!!
品号,员工姓名(空)
B表
员工姓名,品号,产品数量在A表中提取第一行数据,判断,A.品号=B.品号 AND 产品数量最大 把B.员工姓名,插入到A.员工姓名
NET第二行,同上,但是要判定每个员工姓名不能在A表中超过3次!如果超过3次,选产品数量第二大的!!
求解!!!!!!!!
解决方案 »
- 又一个sql问题!!!游标
- 怎么判断字段是否是依次增加的
- 删除表中重复数据
- 我使用vbs 写了一个 function 我想使用一个调用function 中的返回值,如下!
- 动态生成树时,为什么每次会生成两棵一样的树
- 如何才能不插入null呢?
- 求一个简单SQL 递归语句(SQL SERVER) (3贴共120分)
- 从excel导入数据到sqlserver的问题,数据类型转换错误
- 如何把SQL SERVER 7中的数据库完全的平移到SQL SERVER 2000中
- SSRS-ReportServerHttpRuntimeClientDisconnectionException
- 求同一表中某字段更新数据???
- SQL Server 如何计算周合格率,年合格率,月合格率。
select b.员工姓名,b.品号
from b,a
where a.品号=b.品号
and not exists
(select 1 from b as tb where tb.品号=b.品号 and tb.产品数量>b.产品数量)
insert a(员工姓名,品号)
select b.员工姓名,b.品号
from b,a
where a.品号=b.品号
and not exists
(select 1 from b as tb where tb.品号=b.品号 and tb.产品数量>b.产品数量)
and b.员工姓名 not in (select 员工姓名 from a group by 员工姓名 having count(*)>3)
declare @A表 table (品号 int,员工姓名 varchar(10))
insert into @A表
select 1,null union all
select 2,null union all
select 3,nulldeclare @B表 table (员工姓名 varchar(8),品号 int,产品数量 int)
insert into @B表
select 'zhangsan',1,5 union all
select 'lisi',1,4 union all
select 'wangwu',1,6 union all
select 'zhangsan',2,2 union all
select 'lisi',2,4 union all
select 'zhangsan',3,1 union all
select 'lisi',3,4;with maco as(
select row_number() over (partition by b.品号 order by 产品数量 desc) as rid,
b.品号,b.员工姓名,c.个数 from @B表 b
left join (select 品号,count(产品数量) as 个数 from @B表 group by 品号 )c
on b.品号=c.品号
)
update @A表
set 员工姓名=b.员工姓名
from @A表 a left join (
select 品号,员工姓名 from maco where (rid=1 and 个数<3) or(rid=2 and 个数>2)
)b on a.品号=b.品号select * from @A表
/*
品号 员工姓名
----------- ----------
1 zhangsan
2 lisi
3 lisi
*/
insert into @A表
select 1,null union all
select 2,null union all
select 3,nulldeclare @B表 table (员工姓名 varchar(8),品号 int,产品数量 int)
insert into @B表
select 'zhangsan',1,5 union all
select 'lisi',1,4 union all
select 'wangwu',1,6 union all
select 'zhangsan',2,2 union all
select 'lisi',2,4 union all
select 'zhangsan',3,1 union all
select 'lisi',3,4create table #t
(id int identity,员工姓名 varchar(8),品号 int,产品数量 int)
insert into #t
select * from @B表 order by 品号,产品数量 descupdate @A表 set 员工姓名=b.员工姓名 from @A表 a
left join (select 品号,员工姓名 from
(select a.品号,a.员工姓名,rid=
(select count(*) from #t where 品号=a.品号 and id<=a.id),c.个数 from #t a
left join (select 品号,count(产品数量) as 个数 from @B表 group by 品号 )c
on a.品号=c.品号)m where (rid=1 and 个数<3) or(rid=2 and 个数>2)
)b on a.品号=b.品号select * from @A表drop table #t/*
品号 员工姓名
----------- ----------
1 zhangsan
2 lisi
3 lisi
*/