Sql server 2005 SQL-压缩重复boolan';何时';声明
请任何人提出一种压缩此代码的方法,以减少其重复性。非常感谢Sql server 2005 SQL-压缩重复boolan';何时';声明,sql-server-2005,Sql Server 2005,请任何人提出一种压缩此代码的方法,以减少其重复性。非常感谢 select case when c=1 and cs=1 and f=0 and fs=0 then 'FPL02' when c=0 and cs=0 and f=1 and fs=1 then 'FPL03' when c=1 and cs=0 and f=0 and fs=0 then 'FPL04' when c=0 and cs=0 and f=1 and fs=0 then 'FPL
select case
when c=1 and cs=1 and f=0 and fs=0 then 'FPL02'
when c=0 and cs=0 and f=1 and fs=1 then 'FPL03'
when c=1 and cs=0 and f=0 and fs=0 then 'FPL04'
when c=0 and cs=0 and f=1 and fs=0 then 'FPL05'
when c=1 and cs=1 and f=1 and fs=1 then 'FPL06'
when c=1 and cs=1 and f=1 and fs=0 then 'FPL07'
when c=1 and cs=0 and f=1 and fs=1 then 'FPL08'
when c=1 and cs=0 and f=1 and fs=0 then 'FPL09'
when Ab=1 then 'FPL10'
when cpc=1 and plo=0 then 'FPC01'
when cpc=0 and plo=1 then 'FPC02'
when cpc=1 and plo=1 then 'FPC03'
else 'FPL01' end
from (select ptmatter, BillLHAbsolute as Ab, BillLHChildren as C, BillLHChildrenSettle as CS, BillLHFinances as F, BillLHFinancesSettle as FS, BillLHCPC as CPC, BillLHPLO as PLO from MatterDataDef) as mmd
where ptmatter=$matter$
由于在不同的列上有这么多不同的条件语句,我真诚地怀疑您是否能够在让其他人仍然可以维护代码的同时压缩代码 例如,您需要以下内容:
select case
when c IN (0, 1) AND cs IN (0, 1) AND f IN (0, 1) AND fs IN (0, 1) then
case
when c=1 and cs=1 and f=1 and fs=0 then 'FPL07'
when c=1 and cs=0 and f=1 and fs=0 then 'FPL09'
else 'FPL0' + cast(c * 5 + f * 6 - cs * 2 - fs * 2 - 1 as char(1))
end
when Ab = 1 then
'FPL10'
when cpc IN (0, 1) AND plo IN (0, 1) then
'FPC0' + cast(cpc * 1 + plo * 2 as char(1))
else
'FPL01'
end
它是浓缩的(有点),但是你用更少的行来换取更少的可读性
总而言之,当使用语句时,实际上没有那么多的。FPL02到FPL09是唯一可能的组合,还是更多?我有一个压缩代码的想法,但它可能比你现在拥有的更难理解。还有更多,我使用了上面的说明目的-我将在上面添加完整的代码-更新:上面的代码现在是FPL02到FPL09的所有combis的完整数据集ab=0?@rene是的,除了FPL10
ab=0
,所有combis都是,为了清楚起见,FPL10c=0 cs=0 f=0 fs=0
以上的前半部分是有效的,但是当ab=1
时,当cpc IN(0,1)和plo IN(0,1)时,然后“FPC0”+cast(cpc*1+plo*2作为字符(1))
或选择其他“FPL01”
时,我的结果返回为FPL0*,而不是必要的结果。有什么想法吗?这意味着某个地方的数学正在被截断。将varchar(1)
cast更改为更大、更灵活的类型,可能是varchar(3)
或varchar(5)
。问题的原因是,要返回FPL01
则c=0 cs=0 f=0 fs=0
,当c在(0,1)中,cs在(0,1)中,f在(0,1)中,fs在(0,1)中时满足,因此,返回else'FPL0'+cast(c*5+f*6-cs*2-fs*2-1作为字符(1)),而不是FPL01`问题得到解决:当c在(0,1)中,cs在(0,1)中,f在(0,1)中,fs在(0,1)中时,将更改为当c=1或cs=1或f=1或fs=1时,则等。