1

我有一个包含以下值的表。

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

当 substring(B.gl_accountcode,9,2) = '60' 作为条件 1 以及 (B.gl_accountcode) = '40000000003102' 和 substring(B.gl_accountcode,9,2) = 时,我想用 60 填充 MOE 列'60' 表示与条件 2 相同的 GL 参考。

我为这个例子选择了两个 GL 引用“00001064”和“00025928”。

当 substring(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

我想我需要自己加入表格,但我无法理解它。感谢你的帮助。

4

1 回答 1

0

... 60 当 substring(B.gl_accountcode,9,2) = '60' 作为条件 1 以及 (B.gl_accountcode) = '40000000003102' 和 substring(B.gl_accountcode,9,2) = '60' 时与条件 2 相同的 GL 参考。

当 substring(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
于 2013-09-05T10:25:16.197 回答