Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql中使用条件时,每种情况都有不同的限制_Sql_Sql Server_Tsql_Case When - Fatal编程技术网

在sql中使用条件时,每种情况都有不同的限制

在sql中使用条件时,每种情况都有不同的限制,sql,sql-server,tsql,case-when,Sql,Sql Server,Tsql,Case When,我想把限制的情况下,当块的具体条件,是否有一个简单的方法 我有一个这样的sql;我想限制条件4,5,6到50K select SUBSCRIBER_KEY, coalesce( case when pct_1 >90 then case when <condition_1> then 'L_1' when <condition_2> then 'L_2'

我想把限制的情况下,当块的具体条件,是否有一个简单的方法

我有一个这样的sql;我想限制条件4,5,6到50K

select SUBSCRIBER_KEY,
coalesce(
    case when pct_1 >90 then  
                case when <condition_1> then 'L_1' 
                     when <condition_2> then 'L_2'
                     when <condition_3> then 'L_3'
                end 
    else null end,
    case when pct_2<=90 and t.wk_dec_pct<=0.2 
                then
                case when <condition_4> then 'L_4' --to limit output of this condition 50000
                     when <condition_5> then 'L_5' --to limit output of this condition 50000
                     when <condition_6> then 'L_6' --to limit output of this condition 50000
                end
    else null end,
    case when pct_2<=90 and t.wk_dec_pct>=0.2 and t.acc_type='main' 
                then
                case when <condition_7> then 'L_7'
                     when <condition_8> then 'L_8'
                     when <condition_9> then 'L_9'
                end
    else null end                                                
)      
from table_t t
where ...
或者,您可以使用以下类似方法计算L_u值:

case when pct_2<=90 and t.wk_dec_pct<=0.2 
                    then
                    'L_' + CAST((RecordIndex / 50000) + 4 as varchar(2))                    
                    end
        else null end,

我忍不住认为这个查询应该以更好的方式构造。

什么意思?设置限制?我的意思是;我的表中有10mio订阅服务器,当我运行代码时,我在L_4条件下得到120K,我想得到的不是120K,而是50K感谢您的帮助。我尝试了RecordIndex,它带来的结果不到50K,但它没有上限,例如L_5的计数是120K,在添加您的建议后,它带来了33K。我想得到的结果上限为50K*L_1,L_2。。都是象征性的,实际上,我们把长段名称D_1_2_er_max_1_use_和_get_ZV
case when pct_2<=90 and t.wk_dec_pct<=0.2 
                    then
                    'L_' + CAST((RecordIndex / 50000) + 4 as varchar(2))                    
                    end
        else null end,