我有一个包含以下值的表。
gl_accountcode gl_reference gl_subledger_code MOE
40000000003000 00001064 TOTDOO01
40000000003102 00001064 TOTDOO01 60
40002073999807 00001064 TOTDOO01
40000000003000 00025928 WATSEC01
40000000003102 00025928 WATSEC01 60
40305558609795 00025928 WATSEC01 60
当子串(B.gl_accountcode,9,2)='60'作为条件1以及(B.gl_accountcode)='40000000003102'和子串(B.gl_accountcode,9时,我想用60填充MOE列) 2)='60'对于相同的GL参考作为条件2.
我为此示例选取了两个GL参考“00001064”和“00025928”。
当子串(B.gl_accountcode,9,2)不是'60'并且(B.gl_accountcode)='40000000003102'用于相同的GL参考时,我不想在MOE列中填充'60',这似乎正在发生用下面的sql。
select
RTRIM(LTRIM(B.gl_accountcode)) as 'GL AccountCode',
RTRIM(LTRIM(B.gl_reference)) as 'GL Reference',
RTRIM(LTRIM(B.gl_subledger_code)) as 'GL Subledger Code',
CASE
WHEN substring(B.gl_accountcode,9,2) = '60' OR (LTRIM(RTRIM(B.gl_accountcode))) = '40000000003102' THEN '60'
ELSE ''
END MOE
from dbo.pronto_NZD_GLTransUnion B
where B.gl_reference in ( '00025928', '00001064')
order by B.gl_reference, B.gl_accountcode
我想我需要自己加入桌子,但我无法理解它。感谢您的帮助。
答案 0 :(得分:0)
... 60当子串(B.gl_accountcode,9,2)='60'作为条件1时,以及当(B.gl_accountcode)='40000000003102'和子串(B.gl_accountcode,9,2)='时60'对于相同的GL参考作为条件2。
当子串(B.gl_accountcode,9,2)不是'60'且(B.gl_accountcode)='40000000003102'
时,我不想在MOE列中填充'60'
更容易说是......
60 when
substring(x) = '60'
OR
(B.gl_accountcode = 'y' and substring(x) = '60')
(...and not when (B.gl_accountcode = 'y' and substring(x) <> '60')
基本上是
substring(x) = '60'
所以你最后的陈述是:
select
RTRIM(LTRIM(B.gl_accountcode)) as 'GL AccountCode',
RTRIM(LTRIM(B.gl_reference)) as 'GL Reference',
RTRIM(LTRIM(B.gl_subledger_code)) as 'GL Subledger Code',
CASE
WHEN substring(B.gl_accountcode,9,2) = '60'
THEN '60'
ELSE ''
END MOE
from pronto_NZD_GLTransUnion B
where B.gl_reference in ( '00025928', '00001064')
order by B.gl_reference, B.gl_accountcode
编辑:当我在等待你想要的结果时,这可能是你想要的吗?
select
RTRIM(LTRIM(B.gl_accountcode)) as 'GL AccountCode',
RTRIM(LTRIM(B.gl_reference)) as 'GL Reference',
RTRIM(LTRIM(B.gl_subledger_code)) as 'GL Subledger Code',
CASE
WHEN substring(B.gl_accountcode,9,2) = '60' THEN '60'
WHEN gl_accountcode = '40000000003102' AND EXISTS (SELECT 1 FROM pronto_NZD_GLTransUnion a WHERE substring(a.gl_accountcode,9,2) = '60' AND a.gl_reference = B.gl_reference) THEN '60'
ELSE ''
END MOE
from pronto_NZD_GLTransUnion B
where B.gl_reference in ( '00025928', '00001064')
order by B.gl_reference, B.gl_accountcode