WITH CurrencyPrices AS ( SELECT Date(o."updatedAt") as updateddate, o.currency,…
- User
WITH CurrencyPrices AS ( SELECT Date(o."updatedAt") as updated_date, o.currency, o."packId", SUM(amount), SUM(o.price) as total_price FROM "Order" o WHERE o.status IS TRUE GROUP BY updated_date, o."packId", o.currency ) SELECT cp.updated_date, c.name -> c."defaultLang" as name, cp.amount, JSONB_OBJECT_AGG(cp.currency, cp.total_price) as total_price FROM "CurrencyPrices" cp JOIN "Pack" p ON cp."packId" = p.id JOIN "Collection" c ON p."collectionId" = c.id ORDER BY cp.updated_date DESC;
ERROR: relation "CurrencyPrices" does not exist LINE 17: FROM "CurrencyPrices" cp ^ postgres 15버전 sql코드 고쳐
- Assistant
PostgreSQL에서는 "WITH" 절 (Common Table Expression, CTE)을 사용하여 임시 결과 집합을 생성할 수 있으나, 본문의 SQL 문에서 "CurrencyPrices"라는 임시 테이블(CTE)를 정의한 후, 마지막 SELECT 절에서 이를 사용하려고 시도했습니다. 그러나 오류 메시지는 "CurrencyPrices"라는 관계(테이블)이 존재하지 않는다는 것을 나타냅니다. 이것은 기본적으로 임시 테이블에 올바르게 접근하지 못하거나 다른 구문 오류 때문일 수 있습니다.
기술적인 관점에서 보면, SQL 문은 올바른 구조를 가지고 있으나, 실제 오류는 다른 원인에서 나올 수 있습니다. 그러나 제공된 정보에 기반하여, SQL 문을 올바르게 다시 작성해야 할 필요가 있습니다. 여기에 오류 없이 사용할 수 있는 고쳐진 버전을 제안합니다:
WITH CurrencyPrices AS ( SELECT Date(o."updatedAt") AS updated_date, o.currency, o."packId", SUM(o.amount) AS amount, -- 이 부분이 누락되어 있었습니다; 'amount'라는 컬럼에 대한 명시적인 SUM 함수 적용과 별칭 부여 필요 SUM(o.price) AS total_price FROM "Order" o WHERE o.status IS TRUE GROUP BY updated_date, o."packId", o.currency ) SELECT cp.updated_date, c.name ->> c."defaultLang" as name, -- PostgreSQL에서 JSON 필드 접근 시 '->>' 연산자 사용으로 오류 수정이 필요할 수 있음 cp.amount, JSONB_OBJECT_AGG(cp.currency, cp.total_price) as total_price FROM CurrencyPrices cp JOIN "Pack" p ON cp."packId" = p.id JOIN "Collection" c ON p."collectionId" = c.id GROUP BY cp.updated_date, c.name, c."defaultLang", cp.amount -- GROUP BY 절 추가, 필요에 따라 조정 ORDER BY cp.updated_date DESC; - User
WITH CurrencyPrices AS ( SELECT Date(o."updatedAt") as updated_date, c.name -> c."defaultLang" as name, o.currency, SUM(o.amount) as amount, SUM(o.price) as total_price FROM "Order" o JOIN "Pack" p ON p.id = o."packId" JOIN "Collection" c ON c.id = p."collectionId" WHERE o.status IS TRUE GROUP BY updated_date, o.currency, c.name, c."defaultLang" ORDER BY updated_date DESC; ) SELECT cp.updated_date, cp.name, cp.amount, JSONB_OBJECT_AGG(cp.currency, cp.total_price) as total_price FROM CurrencyPrices cp ORDER BY cp.updated_date DESC;
- Assistant
PostgreSQL에서는
WITH절 (Common Table Expression, CTE) 내에서ORDER BY를 사용할 경우, 이는 보통 불필요하거나 CTE의 결과를 직접 정렬하기 위한 목적이 아닙니다. 왜냐하면WITH절은 임시적인 결과 집합을 구성하기 위해 사용되고, 이 결과 집합의 최종 정렬은 보통 최종SELECT문에서 수행됩니다. 따라서, CTE 내에서ORDER BY를 사용하는 것은 최종 결과의 정렬에 직접적인 영향을 주지 않을 수 있습니다.또한,
JSONB_OBJECT_AGG함수를 사용할 때는GROUP BY절이 필요 없습니다만, 당신의 경우name필드를 JSONB 필드 접근 방식으로 선택하고 있고, 이는 CTE 내에서 해결합니다.최종 쿼리에서 크게 잘못된 부분은 없지만, 몇 가지 세밀한 수정이 필요할 수 있습니다. 우선,
ORDER BY updated_date DESC;를 CTE의 끝에서 제거해야 하며, 이는 최종SELECT문에서 충분합니다. 다음은 수정된 버전입니다:WITH CurrencyPrices AS ( SELECT Date(o."updatedAt") AS updated_date, c.name ->> c."defaultLang" AS name, -- '->>' 연산자가 적절한 텍스트 추출을 위해 사용됨 o.currency, SUM(o.amount) AS amount, SUM(o.price) AS total_price FROM "Order" o JOIN "Pack" p ON p.id = o."packId" JOIN "Collection" c ON c.id = p."collectionId" WHERE o.status IS TRUE GROUP BY updated_date, o.currency, c.name, c."defaultLang" -- ORDER BY updated_date DESC; -- 이 줄은 제거. CTE 내에서는 필요 없음 ) SELECT cp.updated_date, cp.name, cp.amount, JSONB_OBJECT_AGG(cp.currency, cp.total_price) AS total_price FROM CurrencyPrices cp GROUP BY cp.updated_date, cp.name, cp.amount -- 'name'과 'amount'도 GROUP BY 절에 포함해야 합니다. ORDER BY cp.updated_date DESC;