根据某些结果更改列值

时间:2016-01-26 08:22:36

标签: sql sql-server sql-server-2008

SELECT BCJTRANS.COMPANYCODE, BCJTRANS.ACCOUNTCODE, BCJTRANS.BOOKCODE, BCJTRANS.MODE,
    BCJTRANS.ENTRYYEAR, BCJTRANS.ENTRYNO, BCJTRANS.AMOUNT
FROM B_C_J_TRANS AS BCJTRANS
WHERE BCJTRANS.COMPANYCODE='RAM' AND BCJTRANS.ENTRYNO='25' AND BCJTRANS.MODE='J'

结果

COMPANYCODE ACCOUNTCODE BOOKCODE    MODE    ENTRYYEAR   ENTRYNO  AMOUNT
RAM         001809      JOURNL       J        16         25      357553.00
RAM         800100      JOURNL       J        16         25     -357553.00
RAM         700100      JOURNL       J        16         25      133414.29
RAM         001809      JOURNL       J        16         25     -133414.29

如果 ACCOUNTCODE 列包含 001809 ,我现在从上述查询得到4个结果 那么我希望 AMOUNT 列的所有4个结果为零

我希望结果为

COMPANYCODE ACCOUNTCODE BOOKCODE    MODE    ENTRYYEAR   ENTRYNO  AMOUNT
RAM         001809      JOURNL       J        16         25      0
RAM         800100      JOURNL       J        16         25      0
RAM         700100      JOURNL       J        16         25      0
RAM         001809      JOURNL       J        16         25      0

还有一个条件  如果 ACCOUNTCODE 包含 001809 那么我希望 AMOUNT 列的所有结果都为零,除了 ACCOUNTCODE 800100 我希望结果为

COMPANYCODE ACCOUNTCODE    BOOKCODE    MODE    ENTRYYEAR   ENTRYNO  AMOUNT
    RAM         001780      JOURNL       J        16         25      0
    RAM         800100      JOURNL       J        16         25   -357553.00
    RAM         700100      JOURNL       J        16         25      0
    RAM         001780      JOURNL       J        16         25      0

4 个答案:

答案 0 :(得分:2)

这是一个非常简单的解决方案:

SELECT 
  BCJTRANS.AMOUNT*
    MIN(CASE WHEN BCJTRANS.ACCOUNTCODE = '001809' THEN 0 ELSE 1 END) over()  AMOUNT
FROM B_C_J_TRANS AS BCJTRANS 
WHERE BCJTRANS.COMPANYCODE='RAM' AND BCJTRANS.ENTRYNO='25' AND BCJTRANS.MODE='J'

编辑:

解决您的新问题:

SELECT 
  BCJTRANS.AMOUNT*
  CASE WHEN BCJTRANS.ACCOUNTCODE = '800100' THEN 1 ELSE
    MIN(CASE WHEN BCJTRANS.ACCOUNTCODE = '001809' THEN 0 ELSE 1 END) over()
  END  AMOUNT
FROM B_C_J_TRANS AS BCJTRANS 
WHERE BCJTRANS.COMPANYCODE='RAM' AND BCJTRANS.ENTRYNO='25' AND BCJTRANS.MODE='J'

答案 1 :(得分:0)

您可以使用case表达式和公用表表达式:

with cte_trans as 
(
    select
        BCJTRANS.COMPANYCODE, 
        BCJTRANS.ACCOUNTCODE, 
        BCJTRANS.BOOKCODE, 
        BCJTRANS.MODE,
        BCJTRANS.ENTRYYEAR, 
        BCJTRANS.ENTRYNO, 
        BCJTRANS.AMOUNT
    from B_C_J_TRANS as BCJTRANS
    where 
        BCJTRANS.COMPANYCODE='RAM' 
        and BCJTRANS.ENTRYNO='25' 
        and BCJTRANS.MODE='J'
)
select
    COMPANYCODE, 
    ACCOUNTCODE, 
    BOOKCODE, 
    MODE,
    ENTRYYEAR, 
    ENTRYNO, 
    case 
        when exists (select * from cte_trans where ACCOUNTCODE like '%001809%') 
          then 0 
        else AMOUNT 
    end as AMOUNT
from cte_trans

答案 2 :(得分:0)

您可以先查看帐户代码栏:

    if exists(select 1 from
    FROM B_C_J_TRANS AS BCJTRANS
    WHERE BCJTRANS.accountcode=18019)

    SELECT BCJTRANS.COMPANYCODE, BCJTRANS.ACCOUNTCODE, BCJTRANS.BOOKCODE, BCJTRANS.MODE,
        BCJTRANS.ENTRYYEAR, BCJTRANS.ENTRYNO, 0 as 'Amount'
    FROM B_C_J_TRANS AS BCJTRANS
    WHERE BCJTRANS.COMPANYCODE='RAM' AND BCJTRANS.ENTRYNO='25' AND BCJTRANS.MODE='J'
return

else

    SELECT BCJTRANS.COMPANYCODE, BCJTRANS.ACCOUNTCODE, BCJTRANS.BOOKCODE, BCJTRANS.MODE,
        BCJTRANS.ENTRYYEAR, BCJTRANS.ENTRYNO, BCJTRANS.AMOUNT
    FROM B_C_J_TRANS AS BCJTRANS
    WHERE BCJTRANS.COMPANYCODE='RAM' AND BCJTRANS.ENTRYNO='25' AND BCJTRANS.MODE='J'

这不会影响检查和执行计算

答案 3 :(得分:0)

IF   EXISTS(SELECT * FROM B_C_J_TRANS WHERE ACCOUNTCODE = '001809' AND COMPANYCODE='RAM' AND ENTRYNO='15' AND MODE='J') 
SELECT     COMPANYCODE, BOOKCODE, MODE, ENTRYYEAR, ENTRYNO, ACCOUNTCODE, AMOUNT * 0 AS AMOUNT
FROM         B_C_J_TRANS AS BCJTRANS
WHERE     (COMPANYCODE = 'RAM')  AND (ENTRYNO = '15') AND (MODE = 'J')
ELSE
SELECT     COMPANYCODE, BOOKCODE, MODE, ENTRYYEAR, ENTRYNO, ACCOUNTCODE, 
                      CASE WHEN BCJTRANS.ACCOUNTCODE = '800100' THEN BCJTRANS.AMOUNT ELSE 0 END AS AMOUNT
FROM         B_C_J_TRANS AS BCJTRANS
WHERE     (COMPANYCODE = 'RAM') AND (ENTRYNO = '15') AND (MODE = 'J')

我尝试了这个查询并获得了最终结果