是否可以将查询合并到案例结构中?

时间:2016-11-18 16:47:10

标签: sql database oracle

所以,我想做的是根据多个属性和多个标准创建一个标志。以下是我想要设置的标志的结构:

select a.*, b.WINNERID,
   case when (QUERY 1) then 1
        when (QUERY 2) then 2
        when (QUERY 3) then 3
        when (QUERY 4) then 4
        else  5 
   end as priority
from   MYTABLE  a 
left outer join MYSECONDTABLE b
on a.ID = b.ID
order by GROUP_ID, priority;

我现在要做的是使用以下查询作为我的案例。查询不一定要查看相同的属性;我们正在研究多个属性,并为每个属性分配一个重要性。

查询1对我们来说是最重要的,查询4是最低的。

我想做的,基本上是根据重要性级别对数据集进行过滤。所以我们说我们的数据集是100条记录;如果案例1解析为30,那么案例2的查询应仅查看剩余的70.在案例1中解决的30将优先级为1作为单独的字段。

查询1

select b.WINNERID, a.*
from MYTABLE a
join MYSECONDTABLE b
on a.ID=b.ID
where WINNERID is not null;

查询2

select GROUP_ID, ID from MYTABLE where COLUMN1 = 'Y' and GROUP_ID in
(
  select tb1.GROUP_ID from MYTABLE tb1 join MYTABLE tb2 on 
    tb1.GROUP_ID = tb2.GROUP_ID
    and to_number(substr(tb1.GROUP_ID,10,3)) =2 
    and tb1.COLUMN1 = 'Y' and tb2.COLUMN1 = 'N'
);

查询3

select distinct GROUP_ID from MYTABLE where COLUMN1 = 'Y' and GROUP_ID in
(
  select tb1.GROUP_ID from MYTABLE tb1 join MYTABLE tb2 on 
    tb1.GROUP_ID = tb2.GROUP_ID
    and to_number(substr(tb1.GROUP_ID,10,3)) =2 
    and tb1.COLUMN1 = 'Y' and tb2.COLUMN1 = 'Y'
    and tb1.ID > tb2.ID
);

查询4

select tb1.GROUP_ID from MYTABLE tb1 join MYTABLE tb2 on 
  tb1.GROUP_ID = tb2.GROUP_ID
  and to_number(substr(tb1.GROUP_ID,10,3)) =2 
  and tb1.LAST_THING like 'WHATEVER%' and (tb2.LAST_THING not like WHATEVER%' or tb2.LAST_THING is null);

我想在显示完整记录时为每个查询设置一个标志。我该如何组合这些?有可能这样做吗?还有其他方法我应该尝试这个吗?

编辑:

基本上这就是我需要的:

If(Query 1)then flag = 1,否则if(Query 2)flag = 2,否则if(Query 3)flag = 3,否则if(Query 4)flag = 4,else flag = 0

1 个答案:

答案 0 :(得分:0)

您无法在CASE语句中使用查询,因为查询未返回返回值。例如,您无法将任何内容与QUERY1进行比较,因为QUERY1不是您可以在表达式中使用的内容。

也许一种方法可以将您的查询用作公用表表达式,并将它们交叉连接到主查询中,如下所示:

WITH QUERY1 AS (select b.WINNERID,
                       a.*
                  from MYTABLE a
                  join MYSECONDTABLE b
                    on a.ID=b.ID
                  where WINNERID is not null),
     QUERY2 AS (select GROUP_ID, ID
                  from MYTABLE
                  where COLUMN1 = 'Y' AND
                        GROUP_ID IN(select tb1.GROUP_ID
                                      from MYTABLE tb1
                                      join MYTABLE tb2
                                        on tb1.GROUP_ID = tb2.GROUP_ID AND
                                            to_number(substr(tb1.GROUP_ID,10,3)) = 2 AND
                                            tb1.COLUMN1 = 'Y' AND
                                            tb2.COLUMN1 = 'N')),
     QUERY3 AS (select distinct GROUP_ID
                  from MYTABLE
                  where COLUMN1 = 'Y' AND
                        GROUP_ID IN (select tb1.GROUP_ID
                                       from MYTABLE tb1
                                       join MYTABLE tb2
                                         ON tb1.GROUP_ID = tb2.GROUP_ID AND
                                            to_number(substr(tb1.GROUP_ID,10,3)) = 2 AND
                                            tb1.COLUMN1 = 'Y' and tb2.COLUMN1 = 'Y' AND
                                            tb1.ID > tb2.ID)),
     QUERY4 AS (select tb1.GROUP_ID
                  from MYTABLE tb1
                  join MYTABLE tb2
                    on tb1.GROUP_ID = tb2.GROUP_ID AND
                       to_number(substr(tb1.GROUP_ID,10,3)) = 2 AND
                       tb1.LAST_THING like 'WHATEVER%' AND
                       (tb2.LAST_THING not like 'WHATEVER%' or tb2.LAST_THING is null))
select a.*,
       b.WINNERID,
       case when q1.WINNERID > 0 then 1
            when q2.GROUP_ID > 0 then 2
            when q3.GROUP_ID > 0 then 3
            when q4.GROUP_ID > 0 then 4
            else 5 
       end as priority
  from MYTABLE  a 
  left outer join MYSECONDTABLE b
    on a.ID = b.ID
  CROSS JOIN QUERY1 q1
  CROSS JOIN QUERY2 q2
  CROSS JOIN QUERY3 q3
  CROSS JOIN QUERY4 q4
  order by GROUP_ID, priority

将此作为示例代码。我不知道您希望在CASE声明中查看实际情况,因此您需要进行适当的更改。

祝你好运。