Reputation: 7241
and good afternoon. Happy to post I haven't needed to post any questions in a bit and have instead started answering. Woo :)
Anyway, my problem here is that I am trying to write a subquery that pulls my vendor, their part, and how much their part is. The vendor may supply the part many times, so I want to AVERAGE the price of the part. Later, I am looking to see about any problem notices against those parts. However, I can't reference the field that I am averaging later on. What do I do?
The SQL looks as follows and is in ORACLE syntax:
WITH
PartVendor AS
(
SELECT PARTNAME, PARTNUM, AVG(PARTPRICE), VENDNAME, VENDNUM
FROM PARTBL
INNER JOIN VENDTBL ON VENDNUM = PARTVENDNUM
GROUP BY PARTNAME, PARTNUM, VENDNAME, VENDNUM
),
PartProbs AS
(
SELECT PartVendor.*, PROBNUM, PROBDESC
FROM PartVendor
INNER JOIN PROBTBL ON PARTNUM = PROBPARTNUM
)
SELECT *
FROM PartProbs
Later on, I will do more. But I keep getting an error on PARTPRICE telling me that it is an invalid identifier. Can anybody help?
Upvotes: 0
Views: 336
Reputation: 1269563
You should name all columns in the CTE:
WITH PartVendor AS (
SELECT PARTNAME, PARTNUM, AVG(PARTPRICE) as AVG_PARTPRICE, VENDNAME, VENDNUM
FROM PARTBL INNER JOIN
VENDTBL ON VENDNUM = PARTVENDNUM
GROUP BY PARTNAME, PARTNUM, VENDNAME, VENDNUM
),
. . .
Upvotes: 1