Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VinayPrajapati's avatar

How i used parent alias in subquery

SELECT
    DATE(order_item_status.status_date) AS status_date,
    DATE(
        DATE_ADD(
            order_item_status.status_date,
            INTERVAL -1 YEAR
        )
    ) AS end_date,
    (
    SELECT
        COUNT(DISTINCT ois.customer_id)
    FROM
        order_item_status AS ois
    WHERE
        (
            ois.status_date BETWEEN DATE_ADD(
                '2021-01-01 00:00:00',
                INTERVAL -1 YEAR
            ) AND '2021-01-01 23:59:59'
        ) AND ois.status_code = 1
) AS COUNT
FROM
    order_item_status
WHERE
    (
        order_item_status.status_date BETWEEN '2021-01-01 00:00:00' AND '2021-01-03 23:59:59'
    ) AND order_item_status.status_code = 1
GROUP BY
    DATE(order_item_status.status_date);

status_date     end_date     count
2021-01-01      2020-01-01   25098
2021-01-02      2020-01-02   25098
2021-01-03      2020-01-03   25098

status_date, end_date in COUNT alias

0 likes
2 replies
MohamedTammam's avatar

As you did but without the table name

WHERE
    (
        status_date BETWEEN '2021-01-01 00:00:00' AND '2021-01-03 23:59:59'
    ) AND status_code = 1
GROUP BY
    DATE(status_date);

Please or to participate in this conversation.