在子查询中重用查询结果

发布于 2024-11-05 20:56:49 字数 1989 浏览 0 评论 0原文

我有这个丑陋的查询...

sum(CASE 
        WHEN effective_from_date < '2011-05-24' THEN (rate * (effective_to_date - '2011-05-24' + 1)) 
        WHEN effective_to_date > '2011-05-28' THEN (rate * ('2011-05-28' - effective_from_date + 1)) 
        ELSE (rate * (effective_to_date - effective_from_date + 1))
    END
    ) as price_cal_rate
        FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28')) 
         AND 
         NOT EXISTS (

         SELECT days_diff FROM (


        SELECT  ((effective_from_date - lag(effective_to_date) OVER (PARTITION BY NULL ORDER BY effective_from_date ASC))) AS days_diff, effective_from_date, effective_to_date
             FROM calendar_event
             WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-26' AND '2011-05-28') OR (effective_to_date BETWEEN '2011-05-26' AND '2011-05-28')) 


        ) AS t WHERE COALESCE(days_diff, 0) > 1 

        ) AND EXISTS (select * from  (
          select min(effective_from_date) as min_date, max(effective_to_date) as max_date FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28'))
        ) as max_min WHERE min_date <= '2011-05-24' and max_date >= '2011-05-28')

查询正在计算日期范围内的速率...查询很好...但是查询中有很多重复...我想知道是否有一个很好的方法来存储这个子查询的结果

FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') 
AND 
            ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28'))  

并在我的查询中使用它......

I have this ugly query....

sum(CASE 
        WHEN effective_from_date < '2011-05-24' THEN (rate * (effective_to_date - '2011-05-24' + 1)) 
        WHEN effective_to_date > '2011-05-28' THEN (rate * ('2011-05-28' - effective_from_date + 1)) 
        ELSE (rate * (effective_to_date - effective_from_date + 1))
    END
    ) as price_cal_rate
        FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28')) 
         AND 
         NOT EXISTS (

         SELECT days_diff FROM (


        SELECT  ((effective_from_date - lag(effective_to_date) OVER (PARTITION BY NULL ORDER BY effective_from_date ASC))) AS days_diff, effective_from_date, effective_to_date
             FROM calendar_event
             WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-26' AND '2011-05-28') OR (effective_to_date BETWEEN '2011-05-26' AND '2011-05-28')) 


        ) AS t WHERE COALESCE(days_diff, 0) > 1 

        ) AND EXISTS (select * from  (
          select min(effective_from_date) as min_date, max(effective_to_date) as max_date FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') AND 
        ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28'))
        ) as max_min WHERE min_date <= '2011-05-24' and max_date >= '2011-05-28')

the query is calculating the rate over a date range....the query is fine...but there is a lot of duplication in the query....I was wondering if there is a nice way to store the result of this sub query somewhere

FROM calendar_event
        WHERE property_rid = (SELECT rid FROM property WHERE web_id = 'T28314') 
AND 
            ((effective_from_date BETWEEN '2011-05-24' AND '2011-05-28')  OR (effective_to_date BETWEEN '2011-05-24' AND '2011-05-28'))  

and use it throughout my query....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-11-12 20:56:49

您可以使用临时表,因为“mu 太短”建议,但如果您只需要单个“主”查询中的结果并且您使用的是 PostgreSQL 8.4 或更高版本,您也可以使用 带有查询

You can use a temp table as "mu is too short" suggested but if you only need the result in a single "main" query and you are using PostgreSQL 8.4 or higher you can also use with queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文