简体   繁体   中英

Oracle SQL - AVG function invalid identifier

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?

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
     ),
     . . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM