【问题标题】:PostgreSQL "Subquery must return only one column" errorPostgreSQL“子查询必须只返回一列”错误
【发布时间】:2019-12-02 09:37:55
【问题描述】:

您好,我正在尝试进行队列研究。我在运行查询时遇到了一些子查询错误的问题。我实际上只能计算重复百分比,但是当我添加新客户的数量和重复的数量时,错误就出现了。我想要这个百分比的详细信息(重复的比率与新客户数量的比率)在我的最终结果中。

非常感谢您的帮助! :)

24-25-26 行

SELECT time_table.*,
(
    WITH new_customers AS
    (
        SELECT DISTINCT
                order_report._customer_id
        FROM order_report

        INNER JOIN
        (
              SELECT DISTINCT _customer_id
              FROM order_report
              WHERE order_report._created_at::timestamp BETWEEN time_table.first_order_start AND time_table.first_order_stop
              AND _order_status = 'paid' AND _order_product_status != 'UNAVAILABLE'
        ) AS period_orders ON period_orders._customer_id = order_report._customer_id

        WHERE _order_status = 'paid' AND _order_product_status != 'UNAVAILABLE'
        GROUP BY order_report._customer_id
        HAVING MIN(order_report._created_at::timestamp) BETWEEN time_table.first_order_start AND time_table.first_order_stop
    )
    SELECT
        COUNT(*) as repeaters,
        (SELECT COUNT(*) FROM new_customers) as new_customers,
        COUNT(*)::float/(SELECT COUNT(*) FROM new_customers) as repeat_percent
        FROM
        (
            SELECT COUNT(*), order_report._customer_id
            FROM order_report
            INNER JOIN new_customers
            ON new_customers._customer_id = order_report._customer_id
            WHERE order_report._created_at::timestamp <= time_table.stop
            AND _order_status = 'paid' AND _order_product_status != 'UNAVAILABLE'
            GROUP BY order_report._customer_id
            HAVING COUNT(*) > 1
        ) AS REPEATS
)
FROM
(
    WITH time_serie AS
    (
        SELECT
            generate_series AS start,
            (generate_series + interval '3 month' - interval '1 second') AS stop
        FROM generate_series('2017-01-01 00:00'::timestamp, '2017-06-30', '1 month')
    ),
    first_order_serie AS
    (
        SELECT
            start AS first_order_start,
            stop AS first_order_stop
        FROM time_serie
    )
SELECT * FROM time_serie, first_order_serie) AS time_table

【问题讨论】:

标签: sql postgresql subquery


【解决方案1】:

我认为您应该划分查询并一一检查。然后你检测出哪个查询是错误的。如果你奉献它,你会再次分享它。我认为您的问题可能是这个查询:

-------------------
FROM
(
    WITH time_serie AS
    (
        SELECT
            generate_series AS start,
            (generate_series + interval '3 month' - interval '1 second') AS stop
        FROM generate_series('2017-01-01 00:00'::timestamp, '2017-06-30', '1 month')
    ),
    first_order_serie AS
    (
        SELECT
            start AS first_order_start,
            stop AS first_order_stop
        FROM time_serie
    )

【讨论】:

    【解决方案2】:

    您的查询以select 开头,因此之后的所有内容都是子查询。

    首先用所有 CTE 编写查询:

    with new_customers as (
          . . .
         ),
         time_serie as (
         ),
         first_order_serie as (
         )
    select . . .
    from . . .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2019-11-10
      相关资源
      最近更新 更多