求一SQL更新语句,各位帮忙~~~~~~~~~~~

lishery 2010-08-17 10:07:40
项目表(cateid,name,attribute)

cateid name attribute
1 全项目 <null>
1 11 0
1 12 1
1 13 2
1 14 12
2 全项目 <null>
2 21 0
2 22 1
2 23 2
3 全项目 <null>
3 31 0
3 32 1
3 33 12

项目表中,每个cateid都有一条name为“全项目”的记录,现在想更新其 attribute属性,
根据数据库中相同cateid的其他记录的attribute值来确定:
若全为0,则其值为0;
若存在1和2,或者12,则其值为12;
若存在1,不存在2和12,则其值为1;
若存在2,不存在1和12,则其值为2。
...全文
220 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2010-08-17
  • 打赏
  • 举报
回复

--


if object_id ('项目表') is not null
drop table 项目表
go
create table 项目表 (cateid int,name nvarchar(10),attribute int)
insert into 项目表 select 1,'全项目',null
union all select 1,'11',0
union all select 1,'12',1
union all select 1,'13',2
union all select 1,'14',12
union all select 2,'全项目',null
union all select 2,'21',0
union all select 2,'22',1
union all select 2,'23',2
union all select 3,'全项目',null
union all select 3,'31',0
union all select 3,'32',1
union all select 3,'33',12
update 项目表 set attribute=case when name='00' then 0
when (charindex('1',name)>0 and charindex('2',name)>0)
then 12
when (charindex('1',name)>0 and (charindex('2',name)<0 or charindex('12',name)<0))
then 1
when (charindex('2',name)>0 and (charindex('1',name)<0 or charindex('12',name)<0))
then 2 else attribute end
select * from 项目表




SQLCenter 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sqlcenter 的回复:]

[/Quote]

没完全短路,sum(distinct attribute) 没问题。

--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(cateid int, name varchar(8), attribute int)
insert into #
select 1, '全项目', null union all
select 1, '11', 0 union all
select 1, '12', 1 union all
select 1, '13', 2 union all
select 1, '14', 12 union all
select 2, '全项目', null union all
select 2, '21', 0 union all
select 2, '22', 1 union all
select 2, '23', 2 union all
select 3, '全项目', null union all
select 3, '31', 0 union all
select 3, '32', 1 union all
select 3, '33', 12

update # set attribute =
case (select sum(distinct attribute) from # where cateid=t.cateid and name <> '全项目')
when 0 then 0
when 1 then 1
when 2 then 2
else 12
end
from # as t where name = '全项目'

select * from #

/*
cateid name attribute
----------- -------- -----------
1 全项目 12
1 11 0
1 12 1
1 13 2
1 14 12
2 全项目 12
2 21 0
2 22 1
2 23 2
3 全项目 12
3 31 0
3 32 1
3 33 12
*/
lishery 2010-08-17
  • 打赏
  • 举报
回复
多谢各位
王向飞 2010-08-17
  • 打赏
  • 举报
回复
--> 测试数据:项目表
if object_id('项目表') is not null
drop table 项目表

---->建表
create table 项目表([cateid] int,[name] varchar(6),[attribute] int)
insert 项目表
select 1,'全项目',null union all
select 1,'11','0' union all
select 1,'12','1' union all
select 1,'13','2' union all
select 1,'14','12' union all
select 2,'全项目',null union all
select 2,'21','0' union all
select 2,'22','1' union all
select 2,'23','2' union all
select 3,'全项目',null union all
select 3,'31','0' union all
select 3,'32','1' union all
select 3,'33','12'



--> 查询结果
update 项目表
set attribute=n.attribute
from 项目表 m ,(select t.cateid,t.name,attribute=(select
case when max(attribute) = 0 then 0
when max(attribute) = 1 then 1
when max(attribute) = 2 and min(attribute) =2 then 2
when max(attribute) = 2 and min(attribute) =0 then 12
when max(attribute) = 2 and min(attribute) =1 then 12
when max(attribute) = 12 then 12 end
from 项目表 where cateid =t.cateid
group by cateid
) FROM 项目表 t)n
where m.cateid=n.cateid
and m.name ='全项目'

select * from 项目表
--> 删除表格
--DROP TABLE 项目表
SQLCenter 2010-08-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sqlcenter 的回复:]

SQL code
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(cateid int, name varchar(8), attribute int)
insert into #
select 1, '全项目', null union all
select 1, '11', 0 u……
[/Quote]

我短路了,忽略这个回复。
SQLCenter 2010-08-17
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(cateid int, name varchar(8), attribute int)
insert into #
select 1, '全项目', null union all
select 1, '11', 0 union all
select 1, '12', 1 union all
select 1, '13', 2 union all
select 1, '14', 12 union all
select 2, '全项目', null union all
select 2, '21', 0 union all
select 2, '22', 1 union all
select 2, '23', 2 union all
select 3, '全项目', null union all
select 3, '31', 0 union all
select 3, '32', 1 union all
select 3, '33', 12

--> 如果attribute只是0、1、2、12这几个值的话,不用那么复杂
update # set attribute =
case (select sum(attribute) from # where cateid=t.cateid and name <> '全项目')
when 0 then 0
when 1 then 1
when 2 then 2
else 12
end
from # as t where name = '全项目'

select * from #

/*
cateid name attribute
----------- -------- -----------
1 全项目 12
1 11 0
1 12 1
1 13 2
1 14 12
2 全项目 12
2 21 0
2 22 1
2 23 2
3 全项目 12
3 31 0
3 32 1
3 33 12
*/
xman_78tom 2010-08-17
  • 打赏
  • 举报
回复

update tb
set attribute = (case
when not exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute<>0)
then 0
when exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute=1)
and exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute=2)
or exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute=12)
then 12
when exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute=1)
and not exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute in (2,12))
then 1
when exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute=2)
and not exists (select * from tb where t.cateid=cateid and name<>'全项目' and attribute in (1,12))
then 2
end)
from tb t
where name='全项目';
华夏小卒 2010-08-17
  • 打赏
  • 举报
回复
--> 测试数据: [项目表]
if object_id('[项目表]') is not null drop table [项目表]
go
create table [项目表] (cateid int,name varchar(6),attribute int)
insert into [项目表]
select 1,'全项目',null union all
select 1,'11',0 union all
select 1,'12',1 union all
select 1,'13',2 union all
select 1,'14',12 union all
select 2,'全项目',null union all
select 2,'21',0 union all
select 2,'22',1 union all
select 2,'23',2 union all
select 3,'全项目',null union all
select 3,'31',0 union all
select 3,'32',1 union all
select 3,'33',12

update [项目表]
set attribute=case when ( not exists(select * from [项目表] where attribute!=0 and cateid=t.cateid) ) then 0
when ( exists(select * from [项目表] where attribute=1 and cateid=t.cateid)) and
( not exists(select * from [项目表] where attribute=2 and cateid=t.cateid)) and
( not exists(select * from [项目表] where attribute=12 and cateid=t.cateid)) then 1
when ( exists(select * from [项目表] where attribute=2 and cateid=t.cateid)) and
( not exists(select * from [项目表] where attribute=1 and cateid=t.cateid)) and
( not exists(select * from [项目表] where attribute=12 and cateid=t.cateid)) then 2
else 12
end
from [项目表] t
where attribute is null


------------------------------------
select * from [项目表]

cateid name attribute
----------- ------ -----------
1 全项目 12
1 11 0
1 12 1
1 13 2
1 14 12
2 全项目 12
2 21 0
2 22 1
2 23 2
3 全项目 12
3 31 0
3 32 1
3 33 12

(13 行受影响)
chuifengde 2010-08-17
  • 打赏
  • 举报
回复
DECLARE @a table(cateid INT,NAME VARCHAR(10),attribute int)
INSERT @a select 1 ,'全项目',null
union all select 1 ,'11', 0
union all select 1 ,'12', 1
union all select 1 ,'13', 2
union all select 1 ,'14', 12
union all select 2 ,'全项目',null
union all select 2 ,'21', 0
union all select 2 ,'22', 1
union all select 2 ,'23', 2
union all select 3 ,'全项目',null
union all select 3 ,'31', 0
union all select 3 ,'32', 1
union all select 3 ,'33', 12
union all select 4 ,'全项目',null
union all select 4 ,'41', 0
union all select 4 ,'42', 1
union all select 4 ,'43', 0

SELECT *,at=CASE WHEN NOT EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute<>0) THEN 0
WHEN EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=1) AND EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=2)
OR EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=12) THEN 12
WHEN EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=1) AND NOT EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=2)
AND NOT EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=12) THEN 1
WHEN EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=2) AND NOT EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=1)
AND NOT EXISTS(SELECT 1 FROM @a WHERE cateid=a.cateid AND attribute=12) THEN 2
END
FROM @a a
--result
/*cateid NAME attribute at
----------- ---------- ----------- -----------
1 全项目 NULL 12
1 11 0 12
1 12 1 12
1 13 2 12
1 14 12 12
2 全项目 NULL 12
2 21 0 12
2 22 1 12
2 23 2 12
3 全项目 NULL 12
3 31 0 12
3 32 1 12
3 33 12 12
4 全项目 NULL 1
4 41 0 1
4 42 1 1
4 43 0 1

(所影响的行数为 17 行)

*/
笔试(erp系统管理员)题目 求解 偶乃一应届毕业生,上星期在网上见到一不错的公司招oracle数据库管理,于是屁颠屁颠跑到那公司,骗过门卫和保安,咨询了搞卫生的阿姨后,见到了人力资源部主管,话都没说上两句,给偶一道题目(erp系统管理员),结果没几个会做的。其实我自己也挺想往erp方向发展,所以打算下个星期再去。 请各位大虾们帮帮忙忙,提点提点。(我在论坛上查了些,资料,还是有好多不会的。) 1。列出 erp供应商及erp产品的名称,版本? (erp供应商是不是:racle erp在咱国内的用户比如,华菱、湘钢、衡钢、承 钢、济钢等等。名称和版本呢?) 2。名词解释 erp,crm,hr,oa,srm,pdm (我只查到了erp,crm,hr的解释,其他的查不到) 3。用一句话说明 bom,采购计划,生产订单,销售订单,物料清单 (查不到 ) 4。写sql语句。 ‘存货档案’(表一): 存货编码(c),存货名称(c),存货规格(c),存货类别(c) ‘发货单’(表二):存货编码(c),数量(n),单价(n) 用sql写出 A:存货类别为'办公用品',发货金额在1000元以下的所有发货单 B: 按存货名称汇总发货单的发货金额 (此题会做一点点,但是写得不全,还得请大家指导) 5 写出erp系统对'销售发货单',‘销售普通发票’生成的会计分录 (完全摸不着头脑)

34,838

社区成员

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

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