Update?

insert2003 2005-10-21 06:26:03
有如下表:
tb_table
c_No c_Type i_Value1 i_Value2 c_Sign
111111 aaa 120 140 2
111111 bbb 120 100 2
111111 ccc 120 100 2
222222 aaa 200 300 2
222222 bbb 200 150 2

将c_Sign更新为1
条件:
按c_No分组;
i_Value1-i_Value2的值是最小的;
如果最小值的有两条记录,则按c_Type降序排,取第一条;
即如下情况:
111111 bbb 120 100 2
111111 ccc 120 100 2
两条的最小值都是20(120-20的结果)
则将ccc的更新为1

总体要得到如下结果:
tb_table
c_No c_Type i_Value1 i_Value2 c_Sign
111111 aaa 120 140 2
111111 bbb 120 100 2
111111 ccc 120 100 1 --更新
222222 aaa 200 300 2
222222 bbb 200 150 1 --更新

恳请各位老大出手!!!
...全文
156 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
huang2005 2005-10-23
  • 打赏
  • 举报
回复
select min(i-value2) from table
gruop by c_no
update table
set c_sign=1
where =(select top 2 from tale)
我是新手 ,不知道对不对 ?
望大家指教!
iwl 2005-10-23
  • 打赏
  • 举报
回复
数量大的话最好放在存储过程里面 临时表处理或表量处理


create PROCEDURE [test] AS

declare @tb table ( C_no varchar(10), c_Type varchar(10))
insert into @tb(C_no ,c_Type) select c_no,max(c_Type) from tb_table a where a.i_Value1-a.i_Value2= (select b.i_Value1-b.i_Value2 from tb_table b where a.c_no=b.c_no and a.c_Type=b.c_Type) group by c_no

update a set c_Sign=1 from tb_table a,@tb b where a.c_no=b.c_no and a.c_Type=b.c_Type
iwl 2005-10-23
  • 打赏
  • 举报
回复
declare @tb table ( C_no varchar(10), c_Type varchar(10), i_Value1 int, i_Value2 int, c_Sign int)
insert @tb
select '111111','aaa',120,90,2 union
select '111111','bbb',120,100,2 union
select '111111','ccc',120,100,2 union
select '222222','aaa',200,120,2 union
select '222222','bbb',200,150,2


update d set c_Sign=1 from @tb d,(select c_no,max(c_Type) c_Type from @tb a where i_Value1-i_Value2= (select i_Value1-i_Value2 from @tb b where a.c_no=b.c_no and a.c_Type=b.c_Type)group by c_no ) c
where d.c_no=c.c_no and d.c_Type=c.c_Type


select * from @tb
insert2003 2005-10-22
  • 打赏
  • 举报
回复
新贴见:
http://community.csdn.net/Expert/topic/4342/4342993.xml?temp=.8621332
insert2003 2005-10-22
  • 打赏
  • 举报
回复
还有

300要改成100


重新开一贴吧
insert2003 2005-10-22
  • 打赏
  • 举报
回复
汗自已一个!

示例数据有问题
将140改成90

tb_table
c_No c_Type i_Value1 i_Value2 c_Sign
111111 aaa 120 90 2
111111 bbb 120 100 2
111111 ccc 120 100 2
222222 aaa 200 300 2
222222 bbb 200 150 2
$扫地僧$ 2005-10-21
  • 打赏
  • 举报
回复
要是绝对值最小的话!
就将上面语句中的 min(i_Value1-i_Value2) 替换成 min(ABS(i_Value1-i_Value2))
$扫地僧$ 2005-10-21
  • 打赏
  • 举报
回复
drop table tb_table
create table tb_table
(

C_no varchar(10),
c_Type varchar(10),
i_Value1 int,
i_Value2 int,
c_Sign int
)
insert tb_table
select '111111','aaa',120,140,2 union
select '111111','bbb',120,100,2 union
select '111111','ccc',120,100,2 union
select '222222','aaa',200,300,2 union
select '222222','bbb',200,150,2 union
select '222222','ccc',200,140,2

update tb_table set c_Sign=1 from tb_table,
(select Ma.C_no,MAx(Ma.c_Type) as c_Type from (select C_no,c_Type, min(i_Value1-i_Value2) as Value from tb_table
group by C_no,c_Type having min(i_Value1-i_Value2)>0) Ma,
(select T.C_no,Min(T.Value) as Value from (select C_no,c_Type, min(i_Value1-i_Value2) as Value from tb_table
group by C_no,c_Type having min(i_Value1-i_Value2)>0) T
group by T.C_no) Mi
where Ma.C_no=Mi.C_no and Ma.Value=Mi.Value
group by Ma.C_no) T where tb_table.C_no=T.C_no and tb_table.c_Type=T.c_Type

select * from tb_table
losa 2005-10-21
  • 打赏
  • 举报
回复
应该是i_Value1-i_Value2的绝对值是最小的吧
happywang11 2005-10-21
  • 打赏
  • 举报
回复
如果存在
222222 aaa 120 100 2
111111 bbb 120 100 2
那么是更新 111111 还是 222222
koposo 2005-10-21
  • 打赏
  • 举报
回复
提问要严谨
zlj113 2005-10-21
  • 打赏
  • 举报
回复
???
111111 aaa 120 140 2


120-140不比120-100还要小吗??

34,590

社区成员

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

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