3

My query:

CREATE VIEW SOME_VIEW2 AS 
(
  SELECT to_char(tbl_albums.album), COUNT(tbl_songs.id) AS "Songs in album"
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica'
  GROUP BY to_char(tbl_albums.album)
);

Error I got:

Error at Command Line:10 Column:12 Error report: SQL Error: ORA-00998: must name this expression with a column alias 00998. 00000 - "must name this expression with a column alias"

This doesn't work, however I previously tried this:

CREATE VIEW SOME_VIEW AS 
(
  SELECT * FROM tbl_albums
);

And this worked fine, want to ask, what is wrong with first query.

2
  • 4
    Give an alias to the column to_char(tbl_albums.album) in your select list. Commented Oct 28, 2013 at 19:57
  • 1
    There is absolutely no need for the parentheses around the actual select statement. Commented Oct 29, 2013 at 9:13

2 Answers 2

10

All columns must be named in a view. Since you have a calculated column, i.e.

to_char(tbl_albums.album)

You need to give it a name, like so:

to_char(tbl_albums.album) "Album"
Sign up to request clarification or add additional context in comments.

Comments

0

try this query :

CREATE VIEW SOME_VIEW2 (album , SongsInAlbum) AS 
(
  SELECT q.alb,q.counter FROM
  (SELECT to_char(tbl_albums.album) as alb, rownum as counter
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica')q
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.