【问题标题】:different limitation for each case when condition in sqlsql条件下每种情况的不同限制
【发布时间】:2014-08-14 11:35:57
【问题描述】:

我想在特定条件下阻塞的情况下进行限制,有没有简单的方法;

我有一个这样的 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 ...

【问题讨论】:

  • 什么是“设置限制”?
  • 我的意思是;我在 table_t 中有 10mio 订阅者,当我运行代码时,我在 L_4 (condition_4) 中得到 120K,我想得到的不是 120K,而是 50K

标签: sql sql-server tsql case-when


【解决方案1】:
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 RecordIndex <= 50000 then 'L_4' --to limit output of this condition 50000
                     when RecordIndex <= 100000 then 'L_5' --to limit output of this condition 50000
                     when RecordIndex <= 150000 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 
(SELECT
    SUBSCRIBER_KEY,
    pct_1,
    pct_2,
    wk_dec_pct,
    acc_type,
    ROW_NUMBER() OVER (ORDER BY MyOrderColumnForConsistency) RecordIndex
FROM    
    table_t) t
where ...

或者,您可以使用类似以下的方法计算 L_ 值:

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,

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

【讨论】:

  • 感谢您的帮助。我尝试了 RecordIndex,它带来的结果小于 50K,但它没有上限,例如 L_5 的计数是 120K,在添加您的建议后它带来了 33K。我希望结果上限为 50K。 *L_1,L_2..是象征性的,实际上,我们把长段名D_1_2_er_max_1_use_and_get_ZV
猜你喜欢
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多