Bank_products Table:-
------ ------- ----------- -------------
itemid revid product_type status
------ ----- ----------- --------
A001 123 banking BA
A011 234 banking BAA
A011 456 Non-banking BAA
E004 786 Non-banking NBA
A012 023 banking BA
A201 123 Non-banking NBA
A301 312 Commercial COM
C405 023 Non-Commercial NCOM
D003 111 Non-Commercial NCOM
AB011 0001 banking BAA
AB011 01246 Non-banking BAA
----------------------------------------
select itemid, revid, product_type,
case when product_type='banking' then 'BA'
when product_type='Non-banking' then 'NBA'
when product_type='banking' or product_type='Non-banking' then 'BAA'
when product_type='Commerical' then 'COM'
when product_type='Non-commercial' then 'NCOM' end
from bank_products
where product_type in ('banking','Non-banking' ,'Commerical' ,'Non-commercial')
group by itemid, revid.
这里有一个itemid,我们可以有多个revid 现在我需要根据select query ...
更新bank_products表中的状态列更新我的更新查询无法正常运行。请帮忙..
UPDATE bank_products
SET STATUS=
(case when product_type='banking' then 'BA'
when product_type='Non-banking' then 'NBA'
when product_type='banking' or product_type='Non-banking' then 'BAA'
when product_type='Commerical' then 'COM'
when product_type='Non-commercial' then 'NCOM' end)
WHERE product_type in ('banking','Non-banking' ,'Commerical' ,'Non-commercial')
group by itemid, revid.
答案 0 :(得分:0)
检查一下,看看是否有效......
UPDATE bank_products
SET STATUS=
(case when product_type='banking' then 'BA'
when product_type='Non-banking' then 'NBA'
when product_type='Commercial' then 'COM'
when product_type='Non-commercial' then 'NCOM' end)
WHERE product_type in ('banking','Non-banking' ,'Commercial' ,'Non-commercial')
答案 1 :(得分:0)
嗨,首先你的选择查询将不起作用,因为你为相同的行放置大小写,你正在检查两个不同的列值,当它来到行A011 234
行时,它将不会发生在任何给定点将banking
,因此它将更新为BA
而不是BAA它不会验证下一行值,以便您将下一行值作为单独的列然后选择将起作用,尝试使用CTE,你应该能够按预期工作。