微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在查询中转义几个?

如何解决如何在查询中转义几个?

我需要重构查询,但是我不知道该怎么做。

我可以看到多次使用相同逻辑的重复,但是我继续在查询之间重复这种查询方式,我觉得这种查询成为我思考sql的主要框架,但我没有想要这个。

您能告诉我该查询的更可接受的变体,以便不再重复思考的方式吗?

在这里

WITH fourth_table AS
(
    WITH third_table AS
    (
        WITH second_table AS
        (
            WITH initial_table AS
            (
                SELECT 
                disTINCT ctr.country_region,EXTRACT (YEAR FROM s.time_id)::int AS calendar_year,chn.channel_desc,SUM(s.amount_sold) OVER (PARTITION BY ctr.country_region||chn.channel_desc||EXTRACT (YEAR FROM s.time_id)) AS amount_sold
                FROM sales s
                JOIN channels chn ON s.channel_id = chn.channel_id
                JOIN customers c ON s.cust_id  = c.cust_id
                JOIN countries ctr ON c.country_id = ctr.country_id
                WHERE ctr.country_region IN ('Americas','Asia','Europe')
                AND
                EXTRACT (YEAR FROM s.time_id)::int IN (1998,1999,2000,2001)
                ORDER BY ctr.country_region,calendar_year,chn.channel_desc
            )
                SELECT country_region,channel_desc,amount_sold,(amount_sold/SUM(amount_sold) OVER (PARTITION BY country_region||calendar_year)*100)::decimal(10,2) AS bychannels
                FROM initial_table
        )
        SELECT *,LAG (bychannels,4) OVER (ORDER BY 6) AS lower_salary
        FROM second_table--correct here smth wrong
    )
    SELECT *,bychannels - lower_salary AS diff FROM third_table
)
SELECT country_region,--'FM     999,999,990D'
LPAD(to_char(amount_sold,'FM999,990 $'),20,' ') AS amount_sold,LPAD(bychannels || ' %',' ') AS "% BY CHANNELS",LPAD(lower_salary || ' %    ',' ') AS "% PREVIoUS PERIOD",diff AS "% DIFF"
FROM fourth_table WHERE calendar_year NOT IN (1998);

解决方法

您将CTE(公用表查询)与子查询混合在一起,with子句的优点通常是可读性:

with initial_table as
(
    SELECT 
    DISTINCT ctr.country_region,EXTRACT (YEAR FROM s.time_id)::int AS calendar_year,chn.channel_desc,SUM(s.amount_sold) OVER (PARTITION BY ctr.country_region||chn.channel_desc||EXTRACT (YEAR FROM s.time_id)) AS amount_sold
    FROM sales s
    JOIN channels chn ON s.channel_id = chn.channel_id
    JOIN customers c ON s.cust_id  = c.cust_id
    JOIN countries ctr ON c.country_id = ctr.country_id
    WHERE ctr.country_region IN ('Americas','Asia','Europe')
    AND
    EXTRACT (YEAR FROM s.time_id)::int IN (1998,1999,2000,2001)
    ORDER BY ctr.country_region,calendar_year,chn.channel_desc
),second_table as
(
    SELECT country_region,channel_desc,amount_sold,(amount_sold/SUM(amount_sold) OVER (PARTITION BY country_region||calendar_year)*100)::decimal(10,2) AS bychannels
    FROM initial_table
),third_table as
(
    SELECT *,LAG (bychannels,4) OVER (ORDER BY 6) AS lower_salary
    FROM second_table--correct here smth wrong
),fourth_table as
(
    SELECT *,bychannels - lower_salary AS diff FROM third_table
)
SELECT country_region,--'FM     999,999,990D'
LPAD(to_char(amount_sold,'FM999,990 $'),20,' ') AS amount_sold,LPAD(bychannels || ' %',' ') AS "% BY CHANNELS",LPAD(lower_salary || ' %    ',' ') AS "% PREVIOUS PERIOD",diff AS "% DIFF"
FROM fourth_table WHERE calendar_year NOT IN (1998);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。