如何在查询中测试嵌套条件?

时间:2016-11-15 15:33:39

标签: sql postgresql

这是我在表格中显示的数据。     我想更新我的表,如下图所示 -

  1. 首先,我必须检查max sodate,然后更新所有匹配的群组,并使用具有max sodate的群集。

  2. 如果sodate不可用,那么我必须检查max sodateany然后更新所有匹配组工作与具有max sodateany的群集。

  3. 如果sodateany不可用,那么我必须检查max lastedate然后更新所有匹配的群组工作与具有max lastedate的群集

  4. I / P表

    work         |cluster       |       sodate      |sodateany          |   lastedate          
    -----------------------------------------------------------------------------------
    1st scenario 
    -------------
    22356894     | 123645       |      2014-05-07   |   2015-05-17      |   2015-03-16               
    22356894     | 126          |      2014-05-05   |   2015-05-18      |   2015-03-17                
    22356894     | 145          |      2014-04-03   |   2015-04-17      |   2015-03-18                
    
    2nd scenario 
    ------------
    
    24432464     | 456          |       -----       |       2015-05-17  |       2015-03-16                
    24432464     | 321          |      2014-05-05   |   2015-05-18      |   2015-03-17                
    24432464     | 215          |      2014-04-03   |   2015-04-17      |   2015-03-18                
    
    3rd scenario
    -------------
    24432423     | 987          |       --          |   2015-05-17      |   2015-03-16                 
    24432423     | 56           |       --          |   2015-05-18      |   2015-03-17                
    
    4th scenario
    -------------
    24432412     |326           |   --              |--                 |2015-03-16                 
    24432412     |576           |   --              | 2015-05-18        |   2015-03-17                 
    
    5th scenario
    -------------|--            |                   |                   |
    24432423     |326           |   --              |--                 |2015-03-16                             
    24432423     |305           |   --              | --                |   2015-03-17                 
    

    o / p看起来像

    此输出通过使用以下方案进行。

    1)首先,我必须检查max sodate,然后更新所有匹配组工作,并使用具有max sodate的群集。

    2)如果sodate不可用,那么我必须检查max sodateany然后更新所有匹配组工作与具有max sodateany的群集。

    3)如果sodateany不可用,那么我必须检查max lastedate然后更新所有匹配的群组工作与具有max lastedate的群集。

    O / P表

    work            |cluster        |       sodate      |      sodateany|               lastedate          
    ----------------------------------------------------------------------------------------------
    1st scenario   
    ------------    
    22356894        |123645         |    2014-05-07     |2015-05-17     |   2015-03-16               
    22356894        |123645         |   2014-05-05      |2015-05-18     |   2015-03-17                
    22356894        |123645         |    2014-04-03     |2015-04-17     |   2015-03-18                
    
    2nd scenario   
    -------------   
    24432464        |321            |   -----           |2015-05-17     |   2015-03-16                
    24432464        |321            |      2014-05-05   |   2015-05-18  |       2015-03-17               
    24432464        |321            |    2014-04-03     |2015-04-17     |   2015-03-18                
    
    3rd scenario 
    ------------    
    24432423        |56             |      --           |   2015-05-17  |       2015-03-16                
    24432423        |56             |       --          |   2015-05-18  |       2015-03-17                
    
    4th scenario    
    ------------    
    24432412        |576            |   --              |--             |   2015-03-16                 
    24432412        |576            |   --              | 2015-05-18    |       2015-03-17                 
    
    5th scenario   
    ------------   
    24432423        |305            |   --              |--             |   2015-03-16                             
    24432423        |305            |   --              | --            |       2015-03-17  
    

    谢谢

0 个答案:

没有答案
相关问题