我正在使用SQL Server 2014.我是SQL Server开发的新手,并且正在满足客户端的要求。我需要一份报告的帮助...
这是我的表LOANS
:
pid c_name ProductFlag Duration Discription
=====================================================
1. Ash p Annual Vehicle loan
2. JOHN GOLD Half Gold Loan
3. Prit Home Annual Home loan
4. Pari P Half Vehicle loan
5. Stacy Home Annual Home loan
6. Meet Gold Annual Gold Loan
7. Mandy Gold Quarterly Gold Loan
8. Rose Home Annual Home loan
9. Rita Tractor Half Tractor loan
10. Lary Education Half Education loan
我想根据Description
值
Productflag
update LOANS
set description = CASE
WHEN productflag = 'p' THEN 'applied for vehicle loan '
WHEN productflag = 'gold' THEN 'applied for gold loan'
同时我想根据description
值更新列Duration
。
无论Productflag
是什么,对于特定的持续时间....(除了产品标志' p'以及' gold'我想要更新如上所述)
我只想根据Description
值更新列Duration
,如下所示:
update LOANS
set description = CASE
WHEN Duration = 'half' THEN 'applied for half year'
WHEN Duration = 'Quarter' THEN 'applied for Quarter year loan'
以下应该是输出
pid c_name ProductFlag Duration Discription
=====================================================
1. Ash p Annual applied for vehicle loan
2. JOHN GOLD Half applied for half year
3. Prit Home Annual Home loan
4. Pari P Half applied for half year
5. Stacy Home Annual Home loan
6. Meet Gold Annual applied for gold loan
7. Mandy Gold Quarterly applied for Quarter year loan
8. Rose Home Annual Home loan
9. Rita Tractor Half applied for half year
10. Lary Education Half applied for half year
请尽快帮助,谢谢!
答案 0 :(得分:2)
在duration
条件之后添加productflag
条件。
CASE
语句为指定的Order中的每个input_expression = when_expression
子句计算WHEN
。
UPDATE loans
SET description =
CASE
WHEN productflag = 'p' THEN 'applied for vehical loan '
WHEN productflag = 'gold' THEN 'applied for gold loan'
WHEN duration = 'half' THEN 'applied for half year'
WHEN duration = 'Quarter' THEN 'applied for Quarter year loan'
...
答案 1 :(得分:0)
从我理解的问题来看,PrDK的上述答案是正确的(https://stackoverflow.com/a/40203237/4689372)。
看到你的输出,有点奇怪,我应该说:)。请使用以下代码进行测试。
UPDATE LOANS
SET DESCRIPTION =
CASE
WHEN Duration = 'Quarterly' AND ProductFlag IN ('P','GOLD') THEN 'applied for Quarter year loan'
WHEN Duration = 'Annual' AND ProductFlag IN ('GOLD') THEN 'applied for ANNUAL loan'
WHEN Duration = 'Half' THEN 'applied for half year'
WHEN ProductFlag NOT IN ('P','GOLD') THEN 'APPLIED FOR ' + ProductFlag + 'LOAN'
END