从所选行中选择行

时间:2012-01-28 06:34:42

标签: oracle oracle11g

我有以下程序语法:

select count(1) from
from
(
  select id,
  CASE
  when a >= 0 and a <= 30 then 'one'    
  when a >= 31 and a <= 60  then 'two'    
  when a >= 61 and a <= 90  then 'three'    
  else 'NO'
  END
  FROM tabel_1 t
  Where
  (
        (
           TO_CHAR(t.aDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                    
                                                                 //cond1
        ) OR
        (
           TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                                 //cond2
        )OR
        (
           TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                                 //cond3
        )

        AND

        (
              p_Type = 'Admin' AND
                            t.ID > 0    //condA
        )OR
        < 0
        (
          SELECT COUNT(1)
                            FROM tab_2 t2
                            WHERE t2.ID = USER_ID AND
                                  t.ID = t2.ID              condB
        )
   )
  )


)

我的意思是以下条款

     Where
     ( (
                  (
              TO_CHAR(t.aDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    


              TO_CHAR(pTo_Date, 'YYYYMMDD')                    
                                                             //cond1
                ) OR
                (
                   TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD')              

                   AND

                        TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                             //cond2
                )OR
                (
                   TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') 

                    AND    
                        TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                             //cond3
                ) )

仅适用于

                       ( (
                        p_Type = 'Admin' AND
                        t.ID > 0    //condA
                       )OR
                         < 0
                       (
                             SELECT COUNT(1)
                             FROM tab_2 t2
                              WHERE t2.ID = USER_ID AND
                              t.ID = t2.ID              condB
                        ) )

// p_type是一个varchar,它有一些要被comapred的值。 USER_ID是一个包含整数值的int。 我想cond1或con2或con3仅适用于condA或condB .. 我的意思是我希望这个结果只显示给管理员或匹配的用户ID为tab_2的id。  我认为这不是正确的。因为它适用于两个OR语句。 任何人都可以提前帮助.. thanx

1 个答案:

答案 0 :(得分:2)

您的子查询目前正在做的是:

cond1 OR cond2 OR (cond3 AND condA) OR condB

也就是说,如果cond1,cond2或condB为真或者cond3和condA都为真,则为真。

但我认为你想要的是:

(cond1 OR cond2 OR cond3) AND (condA OR condB)

只有当cond1,cond2或cond3中的任何一个为真且condA或condB中的任何一个为真时才为真。

如果是这种情况,那么你需要在前3个conds和最后两个conds周围加上括号:

select count(1) from
from
(
  select id,
  CASE
  when a >= 0 and a <= 30 then 'one'    
  when a >= 31 and a <= 60  then 'two'    
  when a >= 61 and a <= 90  then 'three'    
  else 'NO'
  END
  FROM tabel_1 t
  Where
  ( (
        (
           TO_CHAR(t.aDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                    
                                                                 //cond1
        ) OR
        (
           TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                                 //cond2
        )OR
        (
           TO_CHAR(t.bDate, 'YYYYMMDD') BETWEEN TO_CHAR(pFrom_Date, 'YYYYMMDD') AND    
                TO_CHAR(pTo_Date, 'YYYYMMDD')                                                      
                                                                 //cond3
        ) )

        AND

        ( (
              p_Type = 'Admin' AND
                            t.ID > 0    //condA
        )OR
        < 0
        (
          SELECT COUNT(1)
                            FROM tab_2 t2
                            WHERE t2.ID = USER_ID AND
                                  t.ID = t2.ID              condB
        ) )
   )
  )


)