How to reuse a large query without repeating it?

后端 未结 5 1859
别那么骄傲
别那么骄傲 2020-12-31 06:58

If I have two queries, which I will call horrible_query_1 and ugly_query_2, and I want to perform the following two minus operations on them:

5条回答
  •  半阙折子戏
    2020-12-31 07:25

    create view horrible_query_1_VIEW as 
     select .. ...
      from .. .. ..
    
    create view ugly_query_2_VIEW as 
     select .. ...
      from .. .. ..
    

    Then

    (horrible_query_1_VIEW) minus (ugly_query_2_VIEW)
    
    (ugly_query_2_VIEW) minus (horrible_query_1_VIEW)
    

    Or, maybe, with a with clause:

    with horrible_query_1 as (
      select .. .. ..
        from .. .. ..
    ) ,
    ugly_query_2 as (
      select .. .. ..
         .. .. ..
    )
    (select * from horrible_query_1 minus select * from ugly_query_2    ) union all
    (select * from ugly_query_2     minus select * from horrible_query_1)
    

提交回复
热议问题