简体   繁体   中英

how to correctly write an alias in a query SQL oracle

This is my query

CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid , 
(SELECT COUNT(*) as queue 
FROM Waits t1
WHERE t2.code = t1.code AND t1.queue# <= t2.queue#)
FROM Waits t2;

I keep getting this compile error Error at Command Line : 3 Column : 9 00998. 00000 - "must name this expression with a column alias"

although the Oracle SQL developer doesn't indicate any error beforehand. Also I believe that I am using the alias "query" so I really don't understand. Help would be appreciated

Try putting the alias after the subquery instead of after the column name within the subquery.

Although, I'd rewrite the query thus:

CREATE VIEW CourseQueuePositions AS
SELECT t2.code , t2.cid , count(t1.code) queue
FROM Waits t1, Waits t2
WHERE t2.code = t1.code AND t1.queue# <= t2.queue# ;

I don't have access to Oracle now so I can't test this out. But give it a shot.

Upon reviewing your query, it's hard to tell exactly you're trying to do. Can you update the question with example data and an explanation of what you're trying to do?

I have come up with the solution. Previously I added the alias in the inner subqueries SELECT statement, however that wasn't the correct way. What I should have done is to make an alias of the ENTIRE subquery. So something like this...

CREATE VIEW CourseQueuePositions AS 
SELECT t2.code, t2.cid, 
(SELECT COUNT(*) 
FROM Waits t1 
WHERE t1.code = t2.code AND t1.queue# <= t2.queue#) AS queue
FROM Waits t2;

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